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

Thread: Am I reading this correctly...?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Am I reading this correctly...?

    Hello! I am working on a program and I just want to know if I have followed the directions correctly. My code compiles and runs (Jgrasp) but I still feel like I am missing some items in my code? If anyone could take a second look and let me know it will be greatly appreciated.

    Modify the Student class as follows:

    a. Each Student object should also contain the scores for three tests (in addition to the existing instance variables).

    b. Modify the existing constructor so that each test score is set to zero.

    c. Overload the constructor (create an additional Student constructor) so that all instance values are set by parameter values – the three test scores will be set by additional test score values in the parameter list.

    d. Provide a method called setTestScores that accepts two parameters: the test number (1, 2, or 3) and the score.

    e. Provide a corresponding method called getTestScores that accepts one parameter, the test number (1, 2, or 3), and returns the score for that test number.

    f. Provide a method called testAverage that computes and returns the average test score for this student. (Hint: the value returned should be a double. Don’t store the average, calculate it every time you print a student.)

    g. Modify the toString method such that the test scores and average are included in the description of the student.

    3. Modify the StudentBody main method to exercise the new Student methods:

    a. Use each constructor twice to create 2 new Students, for a total of 4. (You can use the existing Students, John and Marsha, with the constructor in 2b; make up addresses and names for 2 new students using the overloaded constructor in 2c.)

    b. Print out each Student object before any changes are made (zero test scores and average are okay).

    c. Use the setTestScores method to change test scores for John and Marsha, the Students with 0 test scores.

    d. Use the getTestScores method to display the scores that were changed for each Student. Print a line that resembles (without quotes): "John’s score for Test 1 is 89."

    e. Print out each Student object after the changes are made.

     
    public class Address
    {
       private String streetAddress, city, state;
       private long zipCode;
     
    // Constructor: Sets up this address with the specified data
     
       public Address (String street, String town, String st, long zip)
       {
          streetAddress = street;
          city = town;
          state = st;
          zipCode = zip;
       }
    // Returns a desciption of this address object
     
       public String toString()
       {
          String result;
     
          result = streetAddress + " \n";
          result += city + ", " + state + " " + zipCode;
     
          return result;
       }
     
    }

     
    public class Student
    {
       private String firstName, lastName;
       private Address homeAddress, schoolAddress;
       private String ts1, ts2,ts3;
       private int t1,t2,t3; 
     
       // Constructor: Sets up this student with the specified values
     
       public Student (String first, String last, Address home, Address school)
       {
          firstName = first;
          lastName = last;
          homeAddress = home;
          schoolAddress = school;
          t1 = 0;
          t2 = 0;
          t3 = 0;
       }
     
       public Student (String first, String last, Address home, Address school,int t1,int t2,int t3)
       {
          firstName = first;
          lastName = last;
          homeAddress = home;
          schoolAddress = school;
          this.t1 = t1;
          this.t2 = t2;
          this.t3 = t3;
       }
     
     
       // Returns a string description of this student object
     
       public void setTestScores(String test,int score)
        {
        if(test.equals("ts1"))
            t1 = score;
        else if(test.equals("ts2"))
            t2 = score;
        else
            t3 = score;
        }
     
        public int getTestScores(String test)
        {
        if(test.equals("ts1"))
            return t1;
        else if(test.equals("ts2"))
            return t2;
        else
            return t3;
        }
     
        public double testAverage()
        {
            double avg = (double)(t1+t2+t3)/3;
            return avg;
        }
     
     
       public String toString()
       {
          String result;
     
          result = firstName + " " + lastName + "\n";
          result+= "Home Address: \n" + homeAddress + "\n";
          result+= "School Address: \n" + schoolAddress;
          result+= "Test1 : " + t1+" ";
          result+= "Test2 : " + t2+" ";
          result+= "Test3 : " + t3+"\n";
          result+= "Average : "+(double)(t1+t2+t3)/3;
     
          return result;
     
        }
    }

    public class StudentBody
    {
    // Creates some Address and Student objects and prints them
     
       public static void main (String[] args)
       {
          Address school = new Address ("800 Lancaster Ave." , "Villanova","PA" , 19085);
     
          Address jHome = new Address  ("21 Jump Street", "Lynchburg","VA", 24551);
          Student john = new Student ("John", "Smith", jHome, school);
          Address mHome = new Address ("123 Main Street", "Euclid", "OH", 44132);
          Student marsha = new Student ("Marsha", "Jones", mHome, school);
     
          Student newJhon = new Student("John2","Smith", jHome, school,73,80,95);
          Student newMarsha = new Student ("Marsha2", "Jones", mHome, school,99,85,65);
     
     
          System.out.println (john);
          System.out.println();
          System.out.println (marsha);
     
          john.setTestScores("ts1",73);
     
          System.out.println();
          System.out.println("John Test Score for Test1 is : "+john.getTestScores("ts1"));
          System.out.println();
     
          marsha.setTestScores("ts2",85);
     
          System.out.println();
          System.out.println("marsha Test Score for Test2 is : "+marsha.getTestScores("ts2"));
          System.out.println();
     
          System.out.println (john);
          System.out.println();
          System.out.println (marsha);
     
          System.out.println();
          System.out.println (newJhon);
          System.out.println();
          System.out.println (newMarsha);
     
     
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I reading this correctly...?

    feel like I am missing some items in my code
    The items in the requirements are nicely listed. Check the items in the list against what the code does to see if there are any that have not been done.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    squirrelmind (July 12th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Am I reading this correctly...?

    I have done that I just wanted someone to take a second look seeing how I have stared and worked on it for hours and am at the point of questioning myself

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I reading this correctly...?

    If this is homework that you will be graded on, how is it fair to other students if you get someone to "pre-grade" your work?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Am I reading this correctly...?

    Its not pre grading.. it is like asking some one to review your draft paper before turning it into your English teacher. Or looking over your power point before showing it to your boss. I would prefer someone knowledgeable to say "yes you might want to review this section" then ask one of my classmates who would not know if I am on the right track as they are just learning also.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I reading this correctly...?

    It's not your work you would be turning in if someone else has had a say in what's in it.

    If you have specific questions about the code, ask them.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Am I reading this correctly...?

    Figured it out.. and yes I was missing a few things. Thank you!

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  2. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  3. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  4. Problem reading correctly from a file
    By stavrakas in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2011, 08:09 AM
  5. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM