Question

The Tasks 1. The class Boat should be extended/modified to include the following: i. A static variable that is a count of the

GIVEN CODES

*****Boat.java*****

import java.util.HashSet;
import java.util.Set;

public class Boat {
    private String name; //private instance variable name of type String
    private String boatClass; //private instance variable boatClass of type String
    private int regNum; //private instance variable regNum of type int
    private Set<String> crew = new HashSet<String>();

    public void setName(String name) {
        this.name = name;
    }
    public void setBoastClass(String boatClass) {
        this.boatClass = boatClass;
    }
    public void setRegNum(int regNum) {
        this.regNum = regNum;
    }
    public String getName() {
        return name;
    }
    public String getBoatClass() {
        return boatClass;
    }
    public int getRegNum() {
        return regNum;
    }
    public Set<String> getCrew() {
        return crew;
    }
    public void addCrew(String crewName) {
        crew.add(crewName);
    }
    //Constructor for initializing the instance variables
    public Boat() {
        name = "unknown"; //initialize name to unknown
        boatClass = "unknown"; //initialize boatClass to unknown
        regNum = -1; //initialize regNum to -1
    }
    @Override
    public String toString() { //toString method for returning the boat object as String with no formal parameters
        return "Boat " + name + ", Class = " + boatClass + ", #Crew = " + crew.size() + ", Registration # = KA " + regNum; //concatenate the name class and regNum to return the String
    }

}

*******BoatMaker.java******

public class BoatMaker {
    public static void main(String[] args) {
        System.out.println("Starting boat application"); //print the message
        Boat myBoat = new Boat(); //defines a variable myBoat of type Boat initialized to a new Boat object
        System.out.println(myBoat); //print the value of myBoat which calls the toString method of Boat Class
        myBoat.setRegNum(6467);
        myBoat.setBoastClass("International 505");
        myBoat.setName("Harmony Blue");
        System.out.println(myBoat);

        Boat myBoat1 = new Boat();
        myBoat1.setName("Australia II");
        myBoat1.addCrew("Anne");
        myBoat1.addCrew("Bob");

        Boat myBoat2 = new Boat();
        myBoat2.setRegNum(6467);
        myBoat2.setBoastClass("International 505");
        myBoat2.setName("Harmony Blue");
        myBoat2.addCrew("Martha");

        System.out.println(myBoat1);
        System.out.println(myBoat2);

        System.out.println(myBoat1.getName());
        System.out.println(myBoat2.getName());

    }
}

Hi there, I need some help here, the task is add some codes to given code above,

I need to make output as bottom of the picture (nothing change with given code). Please add some explanation as well.

It's java and thank you very much.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BoatMaker.java Code:-

//(BoatMaker.java)
public class BoatMaker.java{
public static void main(String[] args) {
System.out.println("Starting boat application"); // print the message
Boat.printTotal();
Boat myBoat = new Boat(); // defines a variable myBoat of type Boat initialized to a new Boat object
System.out.println(myBoat); // print the value of myBoat which calls the toString method of Boat Class
myBoat.setRegNum(6467);
myBoat.setseqNum(2);
myBoat.setBoatClass("International 505");
myBoat.setName("Harmony Blue");
System.out.println(myBoat);
  
//creating object myBoat1.
Boat myBoat1 = new Boat();
//changing name of myBoat1 to Australia II.
myBoat1.setName("Australia II");
//Adding Anne and Bob to myBoat1.
myBoat1.addCrew("Anne");
myBoat1.addCrew("Bob");
  
//creating object myBoat2.
Boat myBoat2 = new Boat();
myBoat2.setRegNum(6467);
myBoat2.setseqNum(2);
myBoat2.setBoatClass("International 505");
myBoat2.setName("Harmony Blue");
//adding Martha to myBoat2.
myBoat2.addCrew("Martha");
  
//printing the value of myBoat1 and myBoat2.
System.out.println(myBoat1);
System.out.println(myBoat2);
  
//printing the names of myBoat1 and myBoat2.
System.out.println(myBoat1.getName());
System.out.println(myBoat2.getName());
Boat.printTotal();
}
}

Boat.java Code:-

//(Boat.java)

import java.util.HashSet;
import java.util.Set;

public class Boat {
private String name;// private instance variable name of type String
private String boatClass; // private instance variable boatClass of type String
private int regNum,seqNum; // private instance variable regNum of type int
//class variable to store number of objects created
private static int count=0;
// adding instance variable crew to store name of crew.
// using set as HashSet to store name by calculating the hash value
// of the name.
private Set<String> crew = new HashSet<String>();

//mutators or setters to set name,BoatClass and RegNum.
public void setName(String name) {
this.name = name;
}

public void setBoatClass(String boatClass) {
this.boatClass = boatClass;

}

public void setRegNum(int regNum) {
this.regNum = regNum;
count=count + 1;
}

public void setseqNum(int seqNum) {
this.seqNum = seqNum;
}
  
//Accessors or getters to get name,BoatClass and RegNum.
public String getName() {
return name;
}

public String getBoatClass() {
return boatClass;
}

public int getRegNum() {
return regNum;

}
public int getseqNum() {
return seqNum;

}

// return the Set crew.
public Set<String> getCrew() {
return crew;
}

// method to add crew name to Set crew.
public void addCrew(String crewName) {
crew.add(crewName);
}

// Constructor for initializing the instance variables
public Boat() {
seqNum = 1;
name = "unknown"; // initialize name to unknown
boatClass = "unknown"; // initialize boatClass to unknown
regNum = -1; // initialize regNum to -1


}

// modified the toString to include the crew tha display the size of set crew
// i.e. number of crew members.
@Override
public String toString() {

// toString method for returning the boat object asString with no formal
// parameters
return "Boat " + name + ", Class = " + boatClass + ", #Crew = " + crew.size() + ", Registration # = KA "
+ regNum + ", Sequence # = "+seqNum; // concatenate the name class
}
public static void printTotal()
{
System.out.println("Total number of boats made = "+count);
}
}

Output Screenshot:-

input Starting boat application Total number of boats made = 0 Boat unknown, class = unknown, #Crew = 0, Registration # = KA

Note: Could you please consider my effort on this work and give up vote. Thank you :)

Add a comment
Know the answer?
Add Answer to:
GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private...

    HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private int empNumber;    private double hoursWorked;    private double payRate;       private static int hospitalEmployeeCount = 0;    //-----------------------------------------------------------------    // Sets up this hospital employee with default information.    //-----------------------------------------------------------------    public HospitalEmployee()    { empName = "Chris Smith"; empNumber = 9999; hoursWorked = 0; payRate =0;               hospitalEmployeeCount++;    }       //overloaded constructor.    public HospitalEmployee(String eName,...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT