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.


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

Results 1 to 3 of 3

Thread: Help with code outputting wrong info

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    Default Help with code outputting wrong info

    This is the setter and getter part of the program;

    public class Student
        private String name, address, major;
        private double gpa;
        public Student(String name, String address, String major)
            this.name = name;
    		  this.address = address;
    		  this.major = major;
    	 //Will return name
        public String getName()
            return name;
    	 //Will return address
        public String getAddress()
            return address;
    	 //Will return major
        public String getMajor()
            return major;
        //Will calculate and return GPA
        public double gpa()
        Random generator = new Random();
        float gpa = (float) (generator.nextDouble() * 4.0 + 0.5);
        return gpa;

    This is the tester

    public class StudentTester
    	public static void main(String[] args)
    		Student student1 = new Student("Kevin","100 Main Street","Spanish");
    		System.out.println(gpa()); //error here
    		Student student2 = new Student("Jim","15 Key Road","Art");
    		Student student3 = new Student("Frank","8302 School Drive","Math");

    Now I'm having two issues, when I run the program without the GPA in the tester part it just outputs with random letters and numbers like so;


    Also, I cannot figure out why I cannot output a random GPA.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Help with code outputting wrong info

    What does Student's toString() method return? That looks an awful lot like what is returned by Object's default toString() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member goldest's Avatar
    Join Date
    Oct 2009
    Pune, India
    Thanked 12 Times in 10 Posts

    Cool Re: Help with code outputting wrong info


    You need to override the toString() method in your Student class and print something sensible inside that.

    The current output is showing the "Classname @ hex version of object's hashcode" which is the default implementaion of Object class's toString method.

    Hope that helps,

    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

Similar Threads

  1. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  2. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  3. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  4. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  5. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM