14 Replies - 3605 Views - Last Post: 27 March 2011 - 09:29 PM Rate Topic: -----

#1 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Need help writing a Java problem

Posted 27 March 2011 - 09:39 AM

Hi everyone, I've been going through the questions in my homework and I can't seem to figure out what I'm supposed to do for this specific question.

Here are the requirements: Modify the Student class from chapter 6 as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Provide a zero-parameter constructor that sets each test score to zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score. Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. The driver class is called TestStudent. Modify the driver class main method to exercise the new Student methods.

My first question would be: What's a zero parameter constructor? Also can anyone give any hints or a simplified version of what exactly I'm supposed to do?

Here is the Student class:
package chap06;

/**
 * Represents a college student.
 * @author Lewis
 * @author Loftus
 * @version 1
 */
public class Student {
    /** First name of this student. */
    private String firstName;

    /** Last name of this student. */
    private String lastName;

    /** Home address of this student. */
    private Address homeAddress;

    /** School address of this student.  Can be shared by other students */
    private Address schoolAddress;

    /**
    * Constructor: Sets up this student with the specified values.
    * @param first The first name of the student
    * @param last The last name of the student
    * @param home The home address of the student
    * @param school The school address of the student
    */
    public Student(String first, String last, Address home, Address school) {
        firstName = first;
        lastName = last;
        homeAddress = home;
        schoolAddress = school;
    }

    /**
    * Returns a string description of this Student object.
    * @return formatted name and addresses of student
    */
    public String toString() {
        String result;

        result = firstName + " " + lastName + "\n";
        result += "Home Address:\n" + homeAddress + "\n";
        result += "School Address:\n" + schoolAddress;

        return result;
    }
}



I'm also getting errors for private Address homeAddress; and private Address schoolAddress; saying Address cannot be resolved to a type.
This was the code provided so I'm not sure why there's errors.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help writing a Java problem

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need help writing a Java problem

Posted 27 March 2011 - 09:54 AM

Parameters are the values in the "argument list" of a function -- that's what's inside the parentheses.

A zero-parameter constructor, or a zero-parameter function, is one that takes NO arguments, e.g.
Student();

The reason for the errors is that if you are using Address as a data type, you have to have a definition for an Address class, just as you need a definition for the Student class in order to instantiate Student objects.

This post has been edited by r.stiltskin: 27 March 2011 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help writing a Java problem

Posted 27 March 2011 - 09:55 AM

A zero parameter constructor is one with no arguments. The constructor for the Student class has 4 arguments.

You will get errors if the Address class has not been defined yet in your source directory.
Was This Post Helpful? 0
  • +
  • -

#4 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 05:23 PM

I'm getting a StackOverflow error when I run this:


Exception in thread "main" java.lang.StackOverflowError
at q3.Student.<init>(Student.java:39)
at q3.Student.<init>(Student.java:40)

package q3;

import java.util.Scanner;

/**
 * <p>
 * This is where you put your description about what this class does. You don't
 * have to write an essay but you should describe exactly what it does.
 * Describing it will help you to understand the programming problem better.
 * </p>
 * 
 * @author Your Name goes here
 * @version 1.0
 */
public class TestStudent {
    /**
     * <p>
     * This is the main method (entry point) that gets called by the JVM.
     * </p>
     * 
     * @param args
     *            command line arguments.
     */
    public static void main(String[] args) {

        System.out.print("Please input your last name: ");
        Scanner scan = new Scanner(System.in);
        String lastName = scan.next();

        System.out.print("Please input your first name: ");
        String firstName = scan.next();

        Address homeAddress = new Address("21 Jump Street", "Langley", "BC",
                "V3A 6K6");

        Address schoolAddress = new Address("3700 Willingdon Ave.", "Burnaby",
                "BC", "V5G 3H2");

        System.out.print("Please input your test score for test 1: ");
        int score1 = scan.nextInt();

        System.out.print("Please input your test score for test 2: ");
        int score2 = scan.nextInt();

        System.out.print("Please input your test score for test 3: ");
        int score3 = scan.nextInt();

        Student s = new Student(lastName, firstName, homeAddress,
                schoolAddress, score1, score2, score3);

        System.out.print(s);

    }

};


Here is the Address class:

package q3;

/**
 * Represents a street address.
 *
 * @author Lewis
 * @author Loftus
 * @version 1
 */
public class Address {
    /** Street address. */
    private String streetAddress;
    /** city. */
    private String city;
    /** State. */
    private String state;
    /** Postal code, only defined for city in US. */
    private String postalCode;

    /**
     * Constructor: Sets up this address with the specified data.
     *
     * @param street
     *        Holds new streetAddress
     * @param town
     *        Holds new city
     * @param st
     *        Holds new state
     * @param code
     *        Holds new postalCode
     */
    public Address(String street, String town, String st, String code) {
        streetAddress = street;
        city = town;
        state = st;
        postalCode = code;
    }

    /**
     * Returns a description of this Address object.
     *
     * @return formatted value of streetAddress, city, state, zipCode
     */
    public String toString() {
        String result;

        result = streetAddress + "\n";
        result += city + ", " + state + "  " + postalCode;

        return result;
    }
}



This post has been edited by for3v3rforgott3n: 27 March 2011 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Need help writing a Java problem

Posted 27 March 2011 - 05:24 PM

Post your new class for student.
Was This Post Helpful? 0
  • +
  • -

#6 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 05:28 PM

View PostManbearpig101, on 27 March 2011 - 05:24 PM, said:

Post your new class for student.


package q3;

/**
 * Represents a college student.
 * @author Lewis
 * @author Loftus
 * @version 1
 */
public class Student {
    /** First name of this student. */
    private String firstName;

    /** Last name of this student. */
    private String lastName;

    /** Home address of this student. */
    private Address homeAddress;

    /** School address of this student.  Can be shared by other students */
    private Address schoolAddress;
    
    private int[] score;

    /**
    * Constructor: Sets up this student with the specified values.
    * 
    * @param first The first name of the student
    * @param last The last name of the student
    * @param home The home address of the student
    * @param school The school address of the student
    */
    
    /**void setTestScore(int testNumber, int score)
    int getTestScore(int testNumber)
    int average()
    String toString()
    */
    
    public Student(String first, String last, Address home, Address school, int score1, int score2, int score3) {
        firstName = first;
        lastName = last;
        homeAddress = home;
        schoolAddress = school;
        score[0] = score1;
        score[1] = score2;
        score[2] = score3;
        
        
    }

    /**
    * Returns a string description of this Student object.
    * @return formatted name and addresses of student
    */
    public String toString() {
        String result;

        result = firstName + " " + lastName + "\n";
        result += "Home Address:\n" + homeAddress + "\n";
        result += "School Address:\n" + schoolAddress;

        return result;
    }
}



Was This Post Helpful? 0
  • +
  • -

#7 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Need help writing a Java problem

Posted 27 March 2011 - 05:34 PM

Right before you start placing elements in the array do:

score = new int[3];



That's the only problem I see in the constructor.
Was This Post Helpful? 1
  • +
  • -

#8 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 05:52 PM

View PostManbearpig101, on 27 March 2011 - 05:34 PM, said:

Right before you start placing elements in the array do:

score = new int[3];



That's the only problem I see in the constructor.


Thanks, that fixed it. I also added the average() method into the Student class.

Any tips on the following? Not too familiar with the terms so I'm kind of confused

You need an overloaded constructor that doesn't take any parameters and sets testScores to 0
Your TestStudent class should test both the constructor that takes parameters and the constructor that takes no parameters
You should also test every method of Student inside your TestStudent class
Was This Post Helpful? 0
  • +
  • -

#9 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Need help writing a Java problem

Posted 27 March 2011 - 06:04 PM

For averages do:

public double getAverage() {
    return (scores[0] + scores[1] + scores[2]) / 3;
}



And for a constructor with no parameters that sets everything to zero just do:

public Student() {
     firstName = "";//Blank first name
     lastName = "";//Blank last name
     homeAddress = null;//Null address, NO ADDRESS
     schoolAddress = null;//Same as previous
     score = new int[3];//Initialize score array, automatically set to 0 (by default)
}



As for the last thing where you have to test everything in the TestStudent class just:

1. Make two students, one using the default constructor "new Student()" and one with the other constructor "new Student(params...etcs)".
2. Show an example of each method (toString() and getAverage()) for each of the new instances of Student you made (the two you just instantiated).
Was This Post Helpful? 0
  • +
  • -

#10 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 07:33 PM

Hey everyone, I have been assigned a couple assignments to do over the weekend and it's taken me a very long time to complete, now I am on my last question with a little over an hour to complete and I'm not sure how to go about doing it.

Here is the requirement:

Design and implement a class called Course that represents a course taken at a school. A Course object should keep track of up to five students, as represented by the modified Student class from the previous project. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter (the Course object should keep track of how many valid students have been added to the course). Provide a method called average that computes and returns the average of all students’ test score averages. Provide a method called roll that prints all students in the course. Create a driver class (called TestCourse) with a main method that creates a course, adds several students, prints a roll, and prints the overall course test average.

Here is the code I have made so far:

Student.java
/**
 * Represents a college student.
 * @author Lewis
 * @author Loftus
 * @version 1
 */
public class Student {
    /** First name of this student. */
    private String firstName;

    /** Last name of this student. */
    private String lastName;

    /** Home address of this student. */
    private Address homeAddress;

    /** School address of this student.  Can be shared by other students */
    private Address schoolAddress;
    
    int[] score = new int[3];
    
    /**
    * Constructor: Sets up this student with the specified values.
    * 
    * @param first The first name of the student
    * @param last The last name of the student
    * @param home The home address of the student
    * @param school The school address of the student
    */
    
    public Student(String first, String last, Address home, Address school, int score1, int score2, int score3) {
        firstName = first;
        lastName = last;
        homeAddress = home;
        schoolAddress = school;
        
        score[0] = score1;
        score[1] = score2;
        score[2] = score3;
        
    }
    
    public Student() {
        score[0] = 0;
        score[1] = 0;
        score[2] = 0;
   }


    /**
    * Returns a string description of this Student object.
    * @return formatted name and addresses of student
    */
    
    public int average() {
        return(score[0] + score[1] + score[2])/score.length;
    }
    
    void setTestScore(int testNum, int testScore) {
        score[testNum] = testScore;
    }
    
    public int getTestScore(int testNum) {
     return score[testNum];   
    }
    
    public String toString() {
        String result;

        result = firstName + " " + lastName + "\n";
        result += "Home Address:\n" + homeAddress + "\n";
        result += "School Address:\n" + schoolAddress + "\n";
        result += "Scores for tests 1,2,3: " + score[0] + ", " + score[1] + ", " + score[2] + "\n";
        result += "Average of the three tests: " + average();

        return result;
    }
}




Address.java (I don't think this is required, but as it is part of the Student.java from the last question, I have to have it for Student.java to work)
package q3;

/**
 * Represents a street address.
 *
 * @author Lewis
 * @author Loftus
 * @version 1
 */
public class Address {
    /** Street address. */
    private String streetAddress;
    /** city. */
    private String city;
    /** State. */
    private String state;
    /** Postal code, only defined for city in US. */
    private String postalCode;

    /**
     * Constructor: Sets up this address with the specified data.
     *
     * @param street
     *        Holds new streetAddress
     * @param town
     *        Holds new city
     * @param st
     *        Holds new state
     * @param code
     *        Holds new postalCode
     */
    public Address(String street, String town, String st, String code) {
        streetAddress = street;
        city = town;
        state = st;
        postalCode = code;
    }

    /**
     * Returns a description of this Address object.
     *
     * @return formatted value of streetAddress, city, state, zipCode
     */
    public String toString() {
        String result;

        result = streetAddress + "\n";
        result += city + ", " + state + "  " + postalCode;

        return result;
    }
}




I do not yet have anything in the main methods of Course and TestCourse and would like some assistant with this specific question, thanks.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help writing a Java problem

Posted 27 March 2011 - 07:39 PM

Topics merged
Your problem seems familiar to me
Please do not double post
Was This Post Helpful? 1
  • +
  • -

#12 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 07:43 PM

View Postpbl, on 27 March 2011 - 07:39 PM, said:

Topics merged
Your problem seems familiar to me
Please do not double post


Sorry, this is not a double post (I think). It's a different question that requires my modified Student.java class as a prerequisite.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help writing a Java problem

Posted 27 March 2011 - 07:57 PM

If you say so, my apologies
but honnestly do not see any difference in your question
Was This Post Helpful? 0
  • +
  • -

#14 for3v3rforgott3n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 17-June 09

Re: Need help writing a Java problem

Posted 27 March 2011 - 08:00 PM

me too, that's the problem =(

From what I'm getting from the requirements, I don't need to touch the scores done in the last question but I need to add 5 students, add an AddStudent method, and have a roll method that prints the course list of students or something

This post has been edited by for3v3rforgott3n: 27 March 2011 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Need help writing a Java problem

Posted 27 March 2011 - 09:29 PM

So where is your Course class? Show us your good faith attempt.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1