Question

In: Computer Science

I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...

I need this in Java please:

Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “green”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Turtle objects. For the first Turtle object, call the first constructor. For the second Turtle object, call the second constructor (passing it a speed of 5 and a color of “purple”). Using the dot ‘.’ Operator, print out the first turtle’s speed and the second turtle’s color. This is a test to see if you can design constructors inside your class and initialize attributes.

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:
Note: If the below code is missing indentation please refer code Images

Typed Code:

// A class called Turtle
class Turtle
{
// An Integer class Attribute or variable named speed
int speed;
// An String class Attribute or variable named color
String color;
  
// default constructor : constructor without parameters
Turtle()
{
// setting default speed value to 0
speed=0;
// setting default color value to green
color = "green";
}
  
// second constructor : with input parameters spd and clr
Turtle(int spd,String clr)
{
// Assigning spd value to class variable speed
speed = spd;
// Assigning clr value to class variable color
color = clr;
}
}

public class Main
{
   public static void main(String[] args)
   {
   // Creating two Turtle Objects
   // Frist Turtle Object with default constructor
       Turtle Turtle_1 = new Turtle();
       // Second Turtle Object with Second constructor(parameterized constructor)
       // with parameters speed 5 and color purple
       Turtle Turtle_2 = new Turtle(5,"purple");
       // Printing Turtle_1 Speed using dot operator
       System.out.println("Turtle 1 Speed: "+ Turtle_1.speed);
       // Printing Turtle_2 Color using dot operator
       System.out.println("Turtle 2 Color: "+ Turtle_2.color);
   }
}
//code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
I need a summary for this article. It may be hard to remember, but there was...
I need a summary for this article. It may be hard to remember, but there was a time when Amazon wasn’t the largest retailer in the U.S. When the company first started selling books online in the 1990s, it had a big hurdle to overcome: getting people to feel comfortable providing their credit card information over the web.One solution that emerged is the digital wallet. A digital wallet is software that allows an e-commerce retailer or a payment service like...
(i just need an answer for question 4) (i just need an answer for just question...
(i just need an answer for question 4) (i just need an answer for just question 4) you have two facilities, one in Malaysia and one in Indonesia. The variable cost curve in the Malaysian plant is described as follows: VCM = q­ + .0005*q2 , where q is quantity produced in that plant per month. The variable cost curve in the Indonesian plant is described by VCI = .5q + .00075q2, where q is the quantity produced in that...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Why there is a specific sound velocity in air atmosphere? Please, for better understanding I need...
Why there is a specific sound velocity in air atmosphere? Please, for better understanding I need a detailed description. May be some examples in nature, schemes, and some description based on basic relations. I want to understand it very well. Would be nice to get some additional sources to read (books or articles).
B has to be matched with A so please I need both in Java. the previous...
B has to be matched with A so please I need both in Java. the previous project mean A A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT