cant run the program with 2 classes
Hi there
i copied below coding from the book.but still cant run the program.i noticed there are two class , they are class student and class obj.i tried separate both class in different class file -> Student.java/Student.class and obj.java/obj.class , but i still cant run the program.Pls help.
Thanks in advance
----------------------------------
Code java:
public class Student {
private String name;
private int matricNo;
private int yearOfBirth;
private int age;
private double fees;
private int numberOfCoursesRegistered;
private String[ ] subjectName;
private boolean registered= false;
public Student(String NAME, int MATRIC, int B_YEAR) {
name= NAME;
yearOfBirth=B_YEAR;
matricNo= MATRIC;
numberOfCoursesRegistered=0;
subjectName= new String[3];
calculateAge();
}
private void calculateAge(){
int currentYear=2008;
age= currentYear-yearOfBirth;
}//calculateAge
public void calculateFee(){
if (registered==true)
fees=numberOfCoursesRegistered*350;
}//calculateFee
public void registerSubject (String subject1){
if (registered==false) {
subjectName[0]=subject1;
numberOfCoursesRegistered=1;
registered=true;
}
else
System.out.println("You already registered");
}// registerSubject
public void registerSubject (String subject1, String subject2){
if (registered==false) {
subjectName[0]=subject1;
subjectName[1]=subject2;
numberOfCoursesRegistered=2;
registered=true;
}
else
System.out.println("You already registered");
}// registerSubject
public void registerSubject (String subject1, String subject2, String subject3){
if (registered==false) {
subjectName[0]=subject1;
subjectName[1]=subject2;
subjectName[3]=subject3;
numberOfCoursesRegistered=3;
registered=true;
}
else
System.out.println("You already registered");
}
// registerSubject
public void displaylnfo ( ) {
if (registered==true){
System.out.println("Name: " + name);
System.out.println("Matric: " + matricNo);
System.out.println("Year of Birth: " + yearOfBirth);
System.out.println("age " +age);
System.out.println("Fees: " + fees);
System.out.println("No of courses registered:+numberOfCoursesRegistered");
System.out.println("Courses registered are:");
for (int count=0; count < numberOfCoursesRegistered; count++)
System.out.println(subjectName[count]);
} //if
else
System.out.println("Information cannot be displayed because you have not registered");
} //displayInfo
}//class
---------------------------------------------------------------------------------------
class obj{
public static void main(String[]args){
Student John = new Student ("John Smith" , 2345 , 1969);
John.registerSubject("Java Programming");
John.calculateFee();
}
}
Re: cant run the program with 2 classes
For future reference, please surround your code with the [highlight=java][/highlight] tags.
Please define "can't run the program". What errors are you receiving? Are the class files in the same directory?
Re: cant run the program with 2 classes
Hi
Thank You so much for your reply.below is the command i typed in DOS and the result.
Yes , i copied both class in same directory -> C:\Program Files\Java\jdk1.6.0_22\bin
-------------------------------------------------------------------------------------------
C:\Program Files\Java\jdk1.6.0_22\bin>java obj.java
Exception in thread "main" java.lang.NoClassDefFoundError: obj/java
Caused by: java.lang.ClassNotFoundException: obj.java
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: obj.java. Program will exit.
C:\Program Files\Java\jdk1.6.0_22\bin>java Student
Exception in thread "main" java.lang.NoSuchMethodError: main
Re: cant run the program with 2 classes
Quote:
Yes , i copied both class in same directory -> C:\Program Files\Java\jdk1.6.0_22\bin
no No No NO NO! It's extremely poor practice to place user files in the program folder tree.
Quote:
C:\Program Files\Java\jdk1.6.0_22\bin>java obj.java
When you give that command, the Java runtime looks for a class named java, which has a package obj; statement and resides in a obj folder subordinate to the current working directory.
http://download.oracle.com/javase/tu...ems/index.html
db
Re: cant run the program with 2 classes
please wirte
{Student st=new student();
ob.yourrequred method.}
Re: cant run the program with 2 classes
Quote:
Originally Posted by
talha07
please wirte
{Student st=new student();
ob.yourrequred method.}
Please don't post garbage.
db