I'm new to java and have a question about default and non default constructors. My professor wants us to use the default constructor for creation of object BOOK1, and then use the non default constructor for BOOK2, BOOK3 and BOOK4. I know a constructor is used with the creation of an object, but I guess I don't understand how i'm supposed to differentiate between the two. My class code is as follows, where I have both a default and non default constructor:

import java.text.DecimalFormat;public final class BOOKItem {DecimalFormat intFormat = new DecimalFormat("000");DecimalFormat doubleFormat = new DecimalFormat("$#,##0.00");private int bookID;private int numberInStock;private double price;private double totalValueOfStock;private int code;private String genre = "";public BOOKItem() {bookID = 0;numberInStock = 0;price = 0;code = 0;}public BOOKItem(int newID, int newStock, double newPrice, int newCode) {setID(newID);setStock(newStock);setCode(newCode);setPrice(newPrice);}public void setStock (int newStock) {if (newStock >= 1 && newStock <=5000) {numberInStock = newStock;}else {numberInStock = 0;}}public void setCode (int newCode) {if (newCode > 0) {code = newCode;}}public void setID (int newID) {if (newID >=11 && newID <= 111111) {bookID = newID;}else {bookID = 0;}}public void setPrice (double newPrice) {if (newPrice >= 1.0 && newPrice <=150.0) {price = newPrice;}else {price = 0;}}public int getID () {return bookID;

}

public int getNumberInStock () {return numberInStock;}public int getCode () {return code;}public double getPrice () {return price;}public double calcTotalValue () {totalValueOfStock = numberInStock * price;return totalValueOfStock;}public double getTotalValue () {return totalValueOfStock;}public void display() {switch (code){ case 1: genre = "Romance";break;case 2: genre = "Adventure";break;case 3:genre = "Sci-Fi";break;case 4:genre = "Mystery";break;} System.out.println("Display:");System.out.println("Book ID: " + bookID + " NumInStock: " + numberInStock + " Code: " + genre + " Price: " + price + " TotalStockValue: " + calcTotalValue());}}

Here is my application that uses the constructors(sorry about the break in the class code, i dunno why its doing that):

import java.util.Scanner;public class Project7 {public static void main(String[] args) {int bookID;int numberInStock;double price;int code;Scanner keyboard = new Scanner(System.in);BOOKItem BOOK1, BOOK2, BOOK3, BOOK4;BOOK1 = new BOOKItem();BOOK2 = new BOOKItem();BOOK3 = new BOOKItem();BOOK4 = new BOOKItem();System.out.println("Enter in a blank separated list: ID, number in stock, quality, price" + "- use a BAD ID(<11 or >111111)");bookID = keyboard.nextInt();numberInStock = keyboard.nextInt();code = keyboard.nextInt();price = keyboard.nextDouble();BOOK1.setID(bookID);BOOK1.setStock(numberInStock);BOOK1.setCode(code);BOOK1.setPrice(price);BOOK1.display();System.out.println("Enter in a blank separated list: ID, number in stock, quality, price" + "- use a BAD STOCK(>5000)");bookID = keyboard.nextInt();numberInStock = keyboard.nextInt();code = keyboard.nextInt();price = keyboard.nextDouble();BOOK2.setID(bookID);BOOK2.setStock(numberInStock);BOOK2.setCode(code);BOOK2.setPrice(price);BOOK2.display();System.out.println("Enter in a blank separated list: ID, number in stock, quality, price" + "- use a BAD PRICE(>150.0)");bookID = keyboard.nextInt();numberInStock = keyboard.nextInt();code = keyboard.nextInt();price = keyboard.nextDouble();BOOK3.setID(bookID);BOOK3.setStock(numberInStock);BOOK3.setCode(code);BOOK3.setPrice(price);BOOK3.display();System.out.println("Enter in a blank separated list: ID, number in stock, quality, price" + "- use ALL GOOD DATA");bookID = keyboard.nextInt();numberInStock = keyboard.nextInt();code = keyboard.nextInt();price = keyboard.nextDouble();BOOK4.setID(bookID);BOOK4.setStock(numberInStock);BOOK4.setCode(code);BOOK4.setPrice(price);BOOK4.display();}}

Am I doing something incorrect with the creation of my objects? How do I use the non default constructor for BOOK2, BOOK3 and BOOK4? I created it, but perhaps i'm using it incorrectly. Any feedback would be greatly appreciated.

1

Best Answer


When you use

 BOOK1 = new BOOKItem();

You are calling the default constructor (Construction without having any arguments)

After taking user input you would call the Non Default Constructor

bookID = keyboard.nextInt();numberInStock = keyboard.nextInt();code = keyboard.nextInt();price = keyboard.nextDouble();BOOK2 = new BOOKItem(bookID, numberInStock, price, code);

Use the above code to use the Parameterized Constructor (Non Default constructor)

This is called constructor overloading.

So for the objects you want to assign default values you call the default constructorFor the object you have information available with variables you sent them in the constructor that you have defined with parameters, To assign that value