Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help with errors

  Asked By: Harry    Date: May 12    Category: Java    Views: 668

I am taking my first Java class. I am
having a couple problems. When compiling the following
code, I get an error on line 14. I am not sure what is
wrong.Also, when I try to run a java.class in dos. I get a
message "Exception in thread "main"
java.lang.NoClassDefFoundErro: Jhw4"The program I am trying to run works at
school, but not at home. I am using win98. Sorry for the
long post. Thanks in advance!Looking for help
in MN. Ryuipublic class Jhw04{ public
static void main (String [] args){ int
numStu=20; if(args.length !=1){ System.out.println("Please use a
number"); System.exit(0); }else{
numStu=Integer.parseInt(args[0]);} Person p = new Person("Perry",
1959); Employee e =
new Employee("Edgar", 1963,
65000.0); System.out.println(p); System.out.println(e); Student[] s
= new
Student[numStu]; for(int i=0;i<s.length;i++){ s[i]=new
Student("Student"+(i+1), (int)(Math.random()*1000000) %20+1970,
(long)(Math.random()*1000000),(int)(Math.random()*1000000)%4+1,"CS"); }
} }/*****class
student****/////class Student extends Person{private String
major;int sid, status;public Student(String nm, int
byear, String m, int id, int s){super(nm,
byear);major=m;sid=id;status=s;}public String
toString(){return "Student[super=" +
super.toString() +", major=" + major + ", sid=" + sid +", status="
+status +"]";}}/***** employee class
*****/class Employee extends Person{double
salary;public Employee(String nm, int byear,double
s){super(nm, byear); salary=s;}public String
toString(){return "Employee[super=" + super.toString() +",salary=
"+ salary +"]";}}/******** class
Person ********************/class Person {
public Person(String nm,int bYear) { name=nm;
birthYear=bYear; } public String getName() { return
name; } public int getBirthYear() { return
birthYear; } public String toString() {
return("Person[name= " + name +",birthyear ="+ birthYear+"]");
} private String name = new String("noname");
private int birthYear=0;}



1 Answer Found

Answer #1    Answered By: Velma Adams     Answered On: May 12

you're calling the Student
constructor with Student(String, int, long, int, String)
but there's no constructor to match that, only
Student(String, int, String, int, int) What I think you
want is this: for(int i=0;i<s.length;i++){
s[i]=new Student("Student"+(i+1),
(int)(Math.random()*1000000) %20+1970, "CS",
(int)(Math.random()*1000000), (int)(Math.random()*1000000)%4+1); }
I've changed the order of the parameters and also
changed the cast to long into a cast to int. As
for the error  running a Java program, you may not
have the classpath set up properly.

Didn't find what you were looking for? Find more on Help with errors Or get search suggestion and latest updates.