Alg Posted September 14, 2011 Share Posted September 14, 2011 This is an assignment from an intro to Java textbook, for a class I'm currently taking on the subject. The point was to teach students how to use constructors. Unfortunately for me, I have very little idea how to do that. [spoiler=Constructor]public class GradeBook { // Instance field private String courseName; private String instructorName; // Constructor public GradeBook( String course, String instructor ) { courseName = course; instructorName = instructor; } // Returns course name public String getCourseName( ) { return courseName; } // Assigns course name public void setCourseName( String course ) { courseName = course; } // Returns instructor name public String getInstructorName( ) { return instructorName; } // Assigns instructor name public void setInstructorName( String instructor ) { instructorName = instructor; } //displayMessage public void displayMessage(){ System.out.printf ("Welcome to the grade book for \n%s!\n Presented by %s. \n", getCourseName(), getInstructorName() ); } } Code[spoiler=Main program]import java.util.Scanner; //Program uses Scanner public class GradeBookTest{ public static void main (String args[]) { Scanner input = new Scanner (System.in); GradeBook gradeBook2 = new GradeBook(); System.out.println ("Please enter a course name:"); String course = input.nextLine(); // Asks user for course name gradeBook2.setCourseName(course); //Sets course name System.out.println ("Please enter the instructor's name:"); String instructor = input.nextLine(); // Asks user for course name gradeBook2.setInstructorName (instructor); //Sets course name System.out.println(); //Blank line gradeBook2.displayMessage(); GradeBook gradeBook1 = new GradeBook(); //Creates the first grade book String courseName; String instructorName; instructorName = "[redacted]"; courseName = "[redacted]"; gradeBook1.displayMessage(); } } I painted some stuff and put it on tumblr Link to comment Share on other sites More sharing options...
sees_all1 Posted September 14, 2011 Share Posted September 14, 2011 First of all, when you create a new class and don't include a constructor, Java does it for you with a blank one.Second, if you specify a new constructor, Java will not supply you with a blank one. With the code you've supplied for GradeBook, in order to create a new GradeBook you must pass in two strings, course and instructor. Try making a blank constructor, such as public GradeBook ( ) { this(null, null); } Then your code should compile. Let me know if you need anything else. 99 dungeoneering achieved, thanks to everyone that celebrated with me! ♪♪ Don't interrupt me as I struggle to complete this thoughtHave some respect for someone more forgetful than yourself ♪♪♪♪ And I'm not doneAnd I won't be till my head falls off ♪♪ Link to comment Share on other sites More sharing options...
obfuscator Posted September 14, 2011 Share Posted September 14, 2011 Yep, it's good programming practice to include a default constructor with no arguments as well as the "proper" one with the arguments you actually want to pass. "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now