Results 1 to 7 of 7
  1. #1
    dead1900 is offline Member
    Join Date
    Oct 2014
    Posts
    11
    Rep Power
    0

    Default Determine the Output but What's the problem here Sir?

    Java Code:
    public class Student{
    	private String lastname;
    	private String firstname;
    	private int age;
    
    	public Student(){
    		this.lastname = "M";
    		this.firstname = "E";
    		this.age = 0;
    	}
    	public Student(String lastname, String firstname, int age){
    		this.lastname = lastname;
    		this.firstname = firstname;
    		this.age = age;
    	}
    	public void displayStudentDetails(){
    		System.out.println("Your first name is: " + firstname);
    		System.out.println("Your last name is: " + lastname);
    		System.out.println("Your age is: " + age);
    	}
    }
    public class TestStudent
    {
    	public static void main(String[] args)
    	{
    		Student p1 = new Student();
    		Student p2 = new Student("Kent", "Clark", 18);
    		p1.displayStudentDetails();
    		p2.displayStudentDetails();
    	}
    }
    Determine the Output but What's the problem here Sir?-asd.png


    I think it's possible to make two public class. I already made another java class but it's not working also. whats should I do ?
    I named my java file as Student .. am I right for that ?? or TestStudent ?


    but If I name TestStudent and name the "public class Student" into "class Student"
    here is the output



    Determine the Output but What's the problem here Sir?-asd.png

    But I don't want "class Student" I want that "public class Student" because thats what we are using
    Last edited by dead1900; 12-16-2014 at 12:20 PM.

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Determine the Output but What's the problem here Sir?

    You can only have one public class per file. So on your case you want one file called Student, with class Student in it, and another file called TestStudent, with the class TestStudent in it.

  3. #3
    dead1900 is offline Member
    Join Date
    Oct 2014
    Posts
    11
    Rep Power
    0

    Default Re: Determine the Output but What's the problem here Sir?

    Quote Originally Posted by trcooke View Post
    You can only have one public class per file. So on your case you want one file called Student, with class Student in it, and another file called TestStudent, with the class TestStudent in it.

    so It's not possible ? how come my school ask that kind of question ?

    and as far as I can remember, he told me that i Should make another file called TestStudent and then run it , but it's not running

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Determine the Output but What's the problem here Sir?

    Then you made a simple mistake, such as not compiling it first or using the wrong class name. Java is already telling you -exactly- what you need to do, you're just not listening. Put the public class TestStudent in a file TestStudent.java. Compile. Run.

    What happens then? Post the results, such as an error you get.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    dead1900 is offline Member
    Join Date
    Oct 2014
    Posts
    11
    Rep Power
    0

    Default Re: Determine the Output but What's the problem here Sir?

    Quote Originally Posted by gimbal2 View Post
    Then you made a simple mistake, such as not compiling it first or using the wrong class name. Java is already telling you -exactly- what you need to do, you're just not listening. Put the public class TestStudent in a file TestStudent.java. Compile. Run.

    What happens then? Post the results, such as an error you get.
    If I name my JAva file into TestStudent , then the ERROR will be the vice verse. Determine the Output but What's the problem here Sir?-1234.png

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Determine the Output but What's the problem here Sir?

    What I say and what the compiler says: put the TestStudent class in a new file TestStudent.java
    What you do: rename the source file you have

    Giggle.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Determine the Output but What's the problem here Sir?

    The Java rules are: One public class per file and the file name has to match the class name.

    So create two files Student.java and TestStudent.java and put the code for each in their respective files. For example:

    Student.java
    Java Code:
    public class Student{
        private String lastname;
        private String firstname;
        private int age;
     
        public Student(){
            this.lastname = "M";
            this.firstname = "E";
            this.age = 0;
        }
        public Student(String lastname, String firstname, int age){
            this.lastname = lastname;
            this.firstname = firstname;
            this.age = age;
        }
        public void displayStudentDetails(){
            System.out.println("Your first name is: " + firstname);
            System.out.println("Your last name is: " + lastname);
            System.out.println("Your age is: " + age);
        }
    }
    TestStudent.java
    Java Code:
    public class TestStudent
    {
        public static void main(String[] args)
        {
            Student p1 = new Student();
            Student p2 = new Student("Kent", "Clark", 18);
            p1.displayStudentDetails();
            p2.displayStudentDetails();
        }
    }
    Now try running your TestStudent class.

Similar Threads

  1. problem with output from Array
    By nellyman in forum New To Java
    Replies: 5
    Last Post: 01-25-2011, 05:41 PM
  2. Problem with a output
    By feastfulsaint in forum Advanced Java
    Replies: 3
    Last Post: 10-25-2010, 02:20 AM
  3. xml output problem
    By tsili in forum XML
    Replies: 2
    Last Post: 05-24-2010, 12:27 AM
  4. Output Problem
    By jazz2k8 in forum New To Java
    Replies: 4
    Last Post: 05-26-2008, 10:59 AM
  5. Output problem
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 06:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •