java联机编译器问题

efzxgjgh  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(305)

我正在皮尔逊我的编程实验室做作业,顺便说一句,我的程序没有结果。然而,在netbeans上,我的应用程序是可靠的,可以编译并提供所需的输出。我浏览了论坛,发现了一个类似的问题,但修复建议不适用于我的应用程序。
作业如下:
设计一个名为 Person 包含用于保存人员姓名、地址和电话号码的字段(全部为 String s) 是的。编写一个初始化所有这些值的构造函数,以及每个字段的mutator和accessor方法。
接下来,设计一个名为 Customer ,它继承自 Person 班级。这个 Customer 班级应该有一个 String 客户编号和 boolean 指示客户是否希望在邮件列表中的字段。编写一个构造函数来初始化这些值,并为类的字段编写相应的mutator和accessor方法。
演示 Customer 在程序中初始化,该程序提示用户输入客户的姓名、地址、电话号码和客户号码的值,然后询问用户该客户是否希望接收邮件。使用此信息创建客户对象,然后打印其信息。
将所有类放在同一个文件中。要做到这一点,不要把它们公开。相反,只需写下:

class  Person { ... }
class  Customer { ... }

提交以下代码后,我收到的错误是:

Driver.java:103: error: class Demo is public, should be declared in a file named Demo.java
     public class  Demo
            ^
1 error

代码:

import java.util.Scanner;

class Person

{

private String name;
private String address;
private String number;

public Person(String name, String address, String number)
{
        super();
        this.name = name;
        this.address = address;
        this.number = number;
}

    public String getName()
{
    return name;

}

public void setName(String name)
{
    this.name = name;

}

public String getAddress()

{
    return address;

}

public void setAddress(String a)

{
    address = a;

}

public String getNumber()
{
    return number;

}

public void setNumber(String number){
 this.number = number;
}

}

class Customer extends Person

{
 private String custNumber;
 private boolean wants;

 public Customer(String name, String address, String number, String         custNumber, boolean wants)
 {
  super(name, address, number);
  this.custNumber = custNumber;
  this.wants = wants;
 }

 public String getcustNumber()
 {
    return custNumber;

 }

 public boolean isWants()
 {
    return wants;
 }

 public void setWants(boolean wants)
 {
   this.wants = wants;
 }
}

/**
 *
 * @author Jonathan
 */

  public class  Tester
 {

   public static void main(String[] args)
   {
        String name, address, number;
        String custNumber;
        String decide;
        boolean wants;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter name of customer:Enter address of     customer:Enter phone number of customer:Enter yes/no -- does the customer want to recieve mail?:");

        name = keyboard.nextLine();

        address = keyboard.nextLine();

        number = keyboard.nextLine();

        custNumber = keyboard.nextLine();

        decide = keyboard.nextLine();
                    wants = decide.equals("yes");

        Customer one = new Customer(name, address, number, custNumber,     wants); // creates new Customer Object.
        System.out.println("Customer: ");
        System.out.println("Name: " + one.getName());
        System.out.println("Address: " + one.getAddress());
        System.out.println("Phone Number: " + one.getNumber());
        System.out.println("Receive Mail?: " + one.isWants());

   }

 }
ou6hu8tu

ou6hu8tu1#

因为tester有main方法,所以应该在tester.java文件中声明它。这将解决你的问题。在上传的代码中看不到演示类。

clj7thdc

clj7thdc2#

import java.util.Scanner;

class Person{
    private String name;
    private String address;
    private String number;

    //Constructors
    public Person(String name, String address, String number){
        this.name = name;
        this.address = address;
        this.number = number;
    }

    public Person(){}

    //Accessors
    public String getName(){
        return this.name;
    }

    public String getAddress(){
        return this.address;
    }

    public String getNumber(){
        return this.number;
    }

    //Mutators
    public void setName(String n){
        this.name = n;
    }

    public void setAddress(String a){
        this.address = a;
    }

    public void setNumber(String n){
        this.number = n;
    }
}

class Customer extends Person{
    private String customerNumber;
    private boolean recieveMail;

    //Constructors
    public Customer(String name, String address, String number, String customerN, boolean rm) {
        super(name, address, number);
        this.customerNumber = customerN;
        this.recieveMail = rm;
    }

    public Customer(){}

    //Accessors
    public String getCustomerNumber(){
        return this.customerNumber;
    }

    public boolean getRecieveMail(){
        return this.recieveMail;
    }

    //Mutators
    public void setCustomerNumber(String c){
        this.customerNumber = c;
    }

    public void setRecieveMail(boolean r){
        this.recieveMail = r;
    }
}

class Driver {

    public static void main(String args[]){

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter name of customer:");
        String name1 = scanner.nextLine();
        System.out.print("Enter address of customer:");
        String address1 = scanner.nextLine();
        System.out.print("Enter phone number of customer:");
        String number1 = scanner.nextLine();
        System.out.print("Enter customer number:");
        String customerNumber = scanner.nextLine();
        System.out.print("Enter yes/no -- does the customer want to recieve mail?:");
        String answer = scanner.nextLine();
        boolean recieveMail = (answer.equals("yes"));

        Customer customer = new Customer(name1, address1, number1, customerNumber, recieveMail);

        System.out.println("\nCustomer: ");
        System.out.println("Name: "+customer.getName());
        System.out.println("Address: "+customer.getAddress());
        System.out.println("Phone Number: "+customer.getNumber());
        System.out.println("Customer Number: "+customer.getCustomerNumber());
        System.out.println("Recieve Mail?: "+customer.getRecieveMail());

    }
}
k4emjkb1

k4emjkb13#

同样的事情也发生在我身上,我为我的编程实验室找到了答案。在显示示例运行的工作区中,您可以看到-sample run#1:java驱动程序。只需将tester for driver更改为类名并删除public。

相关问题