Reputation: 99
The problem is the following. I'm doing a homework for an online programming website called "myprogramminglab". It asks me to do the following exercise:
Design a class named Person with fields for holding a person's name , address, and telephone number (all as Strings ). Write a constructor that initializes all of these values, and mutator and accessor methods for every field.
Next, design a class named Customer, which inherits from the Person class . The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these values and the appropriate mutator and accessor methods for the class 's fields.
Demonstrate the Customer class in a program that prompts the user to enter values for the customer's name, address, phone number, and customer number, and then asks the user whether or not the customer wants to receive mail. Use this information to create a customer object and then print its information.
Put all of your classes in the same file. To do this, do not declare them public.
Instead, simply write:
class Person { ... } class Customer { ... }
However, I did the program first in Eclipse. I put each class in a separate file as you will normally do. After I did that, I moved each class to the website where I am supposed to upload it. I removed the "public
" keyword according to the problem so I'm able to upload it. When I do that, it gives me no result. Like it doesn't even run the program. It compiles, but it does nothing.
I checked an online java compiler and the following is prompted:
Error: Could not find or load main class undefined
What could be the error and how should I fix it?
Here is my code:
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 address) {
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
class Customer extends Person {
private String num;
private boolean wants;
// TODO Auto-generated constructor stub
public Customer(String name, String address, String number, String num, boolean wants) {
super(name, address, number);
this.num = num;
this.wants = wants;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public boolean isWants() {
return wants;
}
public void setWants(boolean wants) {
this.wants = wants;
}
}
class demo {
public static void main(String[] args) {
String name, address, number;
String num;
String decide;
boolean wants;
Scanner get = new Scanner(System.in);
System.out.println("Enter name of customer: ");
name = get.nextLine();
System.out.println("Enter address of customer: ");
address = get.nextLine();
System.out.println("Enter phone number of customer: ");
number = get.nextLine();
System.out.println("Enter customer number: ");
num = get.nextLine();
System.out.println("Enter yes/no -- does the customer want to recieve·mail?: ");
decide = get.nextLine();
if (decide.equals("yes"))
wants = true;
else
wants = false;
Customer one = new Customer(name, address, number, num, wants);
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());
}
}
Upvotes: 0
Views: 1593
Reputation: 348
You need to have one class public which contains the public static main method (in your case, demo). And the file name should be the same as the public class (in your case, demo.java) The other classes can keep them like that (they have package default access actually).
As others also pointeded out, you'd better following the naming convention to change the class name to Demo as well.
Upvotes: 1
Reputation: 3286
Imagine each of your classes has a main function, how should the online compiler know which to make the entry point?
Upvotes: 0