19.8k views
14 votes
The Toy class is started below. Write three constructors for this class as explained in the comments below.

public class Toy
{
private String toyName;
private String toyMaker;
private double toyCost;
//default constructor
//constructor which has two String parameters
// constructor with three parameters
}
2. The Song class is provided below, but the constructors contain errors. Read the comments to determine the number of errors, then find the errors. Each line of code can have zero, one, or more than one errors. If a line of code contains errors, rewrite it correctly to the right.
public class Song
{
private String title;
private String artist;
private int trackMinutes;
private int trackSeconds;
// default constructor contains 2 errors
public void Song ______________________________
{
title = artist = ""; ______________________________
trackMinutes = trackSeconds = 0.0; ______________________________
}
// 1-arg constructor contains 5 errors
public song( title) ______________________________
{
title = title; ______________________________
artist = 0; ______________________________
trackMinutes = ""; ______________________________
trackSecond = 0; ______________________________
}
// 2-arg constructor contains 3 errors
public Song(String t, a) ______________________________
{
t = title; ______________________________
a = artist; ______________________________
trackMinutes = trackSeconds = 0; ______________________________
}
//4-arg constructor contains 4 errors
private Song(String t, String a, int min, int sec); ______________________________
{
title = t; ______________________________
artist = art; ______________________________
trackMin = min; ______________________________
trackSec = sec; ______________________________
}
}

User Noemie
by
4.1k points

2 Answers

9 votes

Final answer:

The Toy class can have three constructors: default, 2-parameter, and 3-parameter constructors. The Song class has errors in its constructors and needs corrections.

Step-by-step explanation:

The Toy class can have three constructors:

  1. A default constructor that takes no parameters and initializes the instance variables to some default values.
  2. A constructor with two String parameters to initialize the toyName and toyMaker variables.
  3. A constructor with three parameters to initialize the toyName, toyMaker, and toyCost variables.

The Song class has errors in its constructors. Here are the corrections:

  1. The default constructor should have no parameters and simply initialize the instance variables.
  2. The 1-arg constructor should have a parameter of type String for the title variable.
  3. The 2-arg constructor should have parameters of type String for t and a.
  4. The 4-arg constructor should have parameters of type String, String, int, and int for t, a, min, and sec respectively.

User Lyn
by
4.7k points
4 votes

Answer:

Following are the solution to this question:

Step-by-step explanation:

For question 1:

public class Toy//defining a class

{ private String toyName;//defining String variable

private String toyMaker;//defining String variable

private double toyCost;//defining double variable

Toy() //defining default constructor Toy

{

}

Toy(String aToyName, String aToyMaker)// defining parameterized constructor that accepts two String parameters

{

}

Toy(String aToyName, String aToyMaker, double aToyCost)// defining parameterized constructor that accepts three parameters

{

}

}

For question 2:

public class Song//defining a class Song

{

private String title;//defining String variable

private String artist;//defining String variable

private int trackMinutes;//defining integer variable

private int trackSeconds;//defining integer variable

Song() //defining default constructor

{

title = artist = "";//use String variable that holds null value

trackMinutes = trackSeconds = 0;//use integer variable that hold a value

}

Song(String title)//use parameterized constructor that holds one parameter

{

this.title = title;// use this keyword to hold title value

artist = "";// //hold value in String variable

trackMinutes = 0;//hold value in integer variable

trackSeconds = 0;//hold value in integer variable

}

Song(String t, String a) //use parameterized constructor that holds two String parameter

{

title=t; //hold value in String variable

artist=a;//hold value in String variable

trackMinutes = trackSeconds = 0; //hold value in integer variable

}

Song(String t, String a, int min, int sec) //use parameterized constructor that takes 2 String and 2 integer parameters

{

title = t;//hold value in integer variable

artist = a;//hold value in String variable

trackMinutes = min; //hold value in integer variable

trackSeconds = sec; //hold value in integer variable

}

}

In the question 1, three constructor is declared, in which one is default and two is parameterized, that accepts two string parameter and two string and one double parameter.

In the question 2, four constructor is declared, in which one is default and three is parameterized, that accepts one, two, and four parameter.

User Dave Taylor
by
4.2k points