Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: cant run the program with 2 classes

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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



    ----------------------------------
    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();
     
    }
    }
    Last edited by copeg; November 14th, 2010 at 08:55 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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?

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: cant run the program with 2 classes

    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.

    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

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant run the program with 2 classes

    please wirte
    {Student st=new student();
    ob.yourrequred method.}
    Last edited by talha07; November 17th, 2010 at 03:43 PM.

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: cant run the program with 2 classes

    Quote Originally Posted by talha07 View Post
    please wirte
    {Student st=new student();
    ob.yourrequred method.}
    Please don't post garbage.

    db

Similar Threads

  1. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM
  2. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  3. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  4. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM