Java: Issues with 0 arg constructors and constructors

1177 views java
4

I'm having lots of issues trying to build a constructor where the variables already have default values, and then call the new variables which I set up later on in the TestGuitar class. I've had success compiling my program and printing the default values. I haven't had success retaining the new ones I tried to set up in the newGuitar variable in the TestGuitar class. I know all the issues are coming from my constructors. In this situation I need a 0 arg constructor that creates a default guitar and a new constructor that creates a very specific guitar.

I set up some private variables for the default guitar. I want my program to return the default variables if I never pass anything to then newGuitar variable in the TestGuitar class. If I do pass something, i.e. guitarLength 24.75, I want my program to return that. Even further, I want that new variable to be callable by a getter method. I believe I have the 75% solution, but I need help specifically with the constructor issues at hand. I just don't think I'm grasping the concepts of constructors enough to incorporate a 0 arg and a specific constructor at the same time.

//File name: Guitar.java
//Autor: Michael Joy
//Date: 09/16/2018
//Purpose: Building and testing guitar objects

import java.awt.Color;
import java.util.*;
import java.util.Random;

class Guitar {
    //defines the default values for our default guitar if we do not pass anything new to the object
    private int numStrings = 6;
    private double guitarLength = 28.2;
    private String guitarManufacturer = "Gibson";
    private Color guitarColor = Color.RED;
    private static int i = 1;
    private static Random rand = new Random();

    //declaring the constructor
    public Guitar (){
        //all my problems coming from here
        this.numStrings = numStrings;
        this.guitarLength = guitarLength;
        this.guitarManufacturer = guitarManufacturer;
        this.guitarColor = guitarColor;
    }

    public Guitar (int strings, double length, String manufacturer, Color color){
        //more problems here
         strings = this.numStrings;
         length = this.guitarLength;
         manufacturer = this.guitarManufacturer;
         color = this.guitarColor;
         System.out.printf("toString: %s \n ", this);
    }

    public int getNumStrings() {
      return numStrings;
    }

    public double getGuitarLength() {
      return guitarLength;
    }

    public String getGuitarManufacturer() {
      return guitarManufacturer;
    }

    public Color getGuitarColor(){
      return this.guitarColor;
    }

    public static void playGuitar(){
        String[] musicNotes = {"A", "B", "C", "D", "E", "F", "G"};
        String[] musicDuration = {"(0.25)","(0.5)","(1)", "(2)","(4)"};
         System.out.print("playGuitar: ");
         for (i = 1; i < 17; i++){
            int index1 = rand.nextInt(musicNotes.length);
            int index2 =  rand.nextInt(musicDuration.length);
            System.out.print(musicNotes[index1]);
            System.out.print(musicDuration[index2] + ",");
            }
         }

    public String toString(){
        return String.format("%d, %f, %s, %s", numStrings, guitarLength, guitarManufacturer, guitarColor);
    }
}

class TestGuitar extends Guitar{
        public static void main (String args[] ){
          Guitar newGuitar = new Guitar(6, 24.75, "Les Paul", Color.white);
          playGuitar();
  }
}

answered question

the program wouldn't even compile. Why don't you fix the compilation error first. Also you are LHS and RHS are incorrect in your constructors.

You have no parameters with which to assign the local fields in the no-args constructor. In your other constructor, you have reversed the fields and the parameters.

I have edited the code to reflect the way I originally learned constructors. If you were to try and use one of my getter methods with this code, you would see that it does not return anything

2 Answers

6

class Guitar {
private static int i = 1;
private static Random rand = new Random();
private int numStrings;
private double guitarLength ;
private String guitarManufacturer;
private Color guitarColor;

//declaring the constructor
public Guitar (){
    //all my problems coming from here
    numStrings = 6;
    guitarLength = 28.2;
    guitarManufacturer = "Gibson";
    guitarColor = Color.RED;
}

public Guitar (int strings, double length, String manufacturer, Color color){
    this.numStrings = Strings;
    this.guitarLength = length;
    this.guitarManufacturer = manufacturer;
    this.guitarColor = color;
     System.out.printf("toString: %s \n ", this);
}
public int getNumStrings() {
  return numStrings;
}

public double getGuitarLength() {
  return guitarLength;
}

public String getGuitarManufacturer() {
  return guitarManufacturer;
}

public Color getGuitarColor(){
  return this.guitarColor;
}

public static void playGuitar(){
    String[] musicNotes = {"A", "B", "C", "D", "E", "F", "G"};
    String[] musicDuration = {"(0.25)","(0.5)","(1)", "(2)","(4)"};
     System.out.print("playGuitar: ");
     for (i = 1; i < 17; i++){
        int index1 = rand.nextInt(musicNotes.length);
        int index2 =  rand.nextInt(musicDuration.length);
        System.out.print(musicNotes[index1]);
        System.out.print(musicDuration[index2] + ",");
        }
     }

public String toString(){
    return String.format("%d, %f, %s, %s", numStrings, guitarLength, guitarManufacturer, guitarColor);
}
}

You could also use 1 contructor:

public Guitar (int strings=6, double length=28.2, String manufacturer="Gibson", Color color=Color.RED){
        this.numStrings = Strings;
        this.guitarLength = length;
        this.guitarManufacturer = manufacturer;
        this.guitarColor = color;
         System.out.printf("toString: %s \n ", this);
    }

posted this
11

Your default initial values belong in the no-args constructor. Then you need to correct the order in which you assign the values in the second constructor. Like,

private int numStrings;
private double guitarLength;
private String guitarManufacturer;
private Color guitarColor;
private static int i = 1;
private static Random rand = new Random();

// declaring the constructor
public Guitar() {
    this.numStrings = 6;
    this.guitarLength = 28.2;
    this.guitarManufacturer = "Gibson";
    this.guitarColor = Color.RED;
}

public Guitar(int strings, double length, String manufacturer, Color color) {
    this.numStrings = strings;
    this.guitarLength = length;
    this.guitarManufacturer = manufacturer;
    this.guitarColor = color;
    System.out.printf("toString: %s \n ", this);
}

Note the first constructor could be re-written to use the other constructor. Like,

// declaring the constructor
public Guitar() {
    this(6, 28.2, "Gibson", Color.RED);
}

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories