Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java help

Featured Replies

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

}
}

 

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

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.

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Create an account or sign in to comment

Important Information

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.