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 3 of 3

Thread: Help with code outputting wrong info

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    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;
    		  gpa();
        }
     
    	 //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(student1.toString());
    		System.out.println(gpa()); //error here
     
    		Student student2 = new Student("Jim","15 Key Road","Art");
    		System.out.println(student2.toString());
     
    		Student student3 = new Student("Frank","8302 School Drive","Math");
    		System.out.println(student3.toString());
    	}
    }


    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;

    Student@9304b1
    Student@190d11
    Student@a90653

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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    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
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Cool Re: Help with code outputting wrong info

    OP,

    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,

    Goldest
    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