Jump to content

Java help


Alg

Recommended Posts

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();

}
}

 

Link to comment
Share on other sites

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 thought
Have some respect for someone more forgetful than yourself ♪♪

♪♪ And I'm not done
And I won't be till my head falls off ♪♪

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.