Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Following the steps and Using Java OO language to create java classes and subcla

ID: 3905433 • Letter: F

Question

Following the steps and Using Java OO language to create java classes and subclasses. Each concrete class need to have a toString() method to define that prints out its attributes.

The attributes must be private! Error checking is necessary.

Step 1:Define an interface Contact 1. That has the following methods (notice that some are for type String, this means you'll have to convert them to int's) 1. getLength/setLength (int) 2. getSpeed/setSpeed (int) 3. setSpeed(String) 4. getName/setName (String) 5. getType/setType (String) (This is an arbitrary string label for anything of class Contact) Step 2: Define an abstract class Ship that implements the Contact Interface. The methods in contact should be defined (no longer abstract, but they can be overriden later on). Step 3: Define a class Destroyer that subclasses Ship 1. that has the following attributes and get/set methods. Supports int and String setNumberMissiles() arguments. If the String argument of setNumberMissiles(0 encounters a parsing error, set the numberMissiles to 2. 1. numberMissile

Explanation / Answer

Solution:

Please find below all the required classes along with the test class

Contact.java

public interface Contact {

int getLength();
void setLength(int len);
int getSpeed();
void setSpeed(int sp);
void setSpeed(String sp);
String getType();
void setType(String ty);
}

Ship.java

public abstract class Ship implements Contact {

private int lenght;
private int speed;
  
public Ship(int lenght, int speed) {
this.lenght = lenght;
this.speed = speed;
}

public int getLength() {
return this.lenght;
}

public void setLength(int len) {
this.lenght = len;
}

public int getSpeed() {
return this.speed;
}

public void setSpeed(int sp) {
this.speed = sp;
}

public void setSpeed(String sp) {
this.speed = Integer.parseInt(sp);
}

public String getType() {
return this.getClass().toString();
}

public void setType(String ty) {
//Please clarify what to set
//The above getClass will suffice the requirement

}

}

Destroyer.java

public class Destroyer extends Ship {

private int numberOfMissiles;

public Destroyer(int numberOfMissiles, int lenght, int speed) {
super(lenght, speed);
this.numberOfMissiles = numberOfMissiles;
}

public int getNumberOfMissiles() {
return this.numberOfMissiles;
}

public void setNumberOfMissile(int missiles) {
this.numberOfMissiles = missiles;
}

public void setNumberOfMissiles(String missiles) {
try{
this.numberOfMissiles = Integer.parseInt(missiles);
}catch(NumberFormatException e){
this.numberOfMissiles = 2;
}
}

@Override
public String toString() {
return "Destroyer [numberOfMissiles=" + numberOfMissiles + ", lenght=" + super.getLength() + ", speed="
+ super.getSpeed() + "]";
}

  
}

Submarine.java

public class Submarine extends Ship {

private int numberOfTorpedos;

public Submarine(int lenght, int speed, int numberOfTorpedos) {
super(lenght, speed);
this.numberOfTorpedos = numberOfTorpedos;
}

public int geNumberOfTorpedos() {
return this.numberOfTorpedos;
}

public void setNumberOfTorpedos(int torpedos) {
this.numberOfTorpedos = torpedos;
}

public void setNumberOfTorpedos(String torpedos) {
try{
this.numberOfTorpedos = Integer.parseInt(torpedos);
}catch(NumberFormatException e){
this.numberOfTorpedos = 2;
}
}

@Override
public String toString() {
return "Submarine [numberOfTorpedos=" + numberOfTorpedos + ", lenght=" + super.getLength() + ", speed="
+ super.getLength() + "]";
}

}

Aircraft.java

public abstract class Aircraft implements Contact {

private int altitude;

public Aircraft(int altitude) {
this.altitude = altitude;
}

public int getAltitude() {
return this.altitude;
}

public void setAltitude(int alt) {
this.altitude = alt;
}
}

P3.java

public class P3 extends Aircraft {

private int numberEngines;
private int lenght;
private int speed;

public P3(int altitude, int lenght, int speed, int numberEngines) {
super(altitude);
this.lenght = lenght;
this.speed = speed;
this.numberEngines = numberEngines;
}
public int getNumberEngines() {
return this.numberEngines;
}

public void setNumberEngines(int no) {
this.numberEngines = no;
}
  
@Override
public int getLength() {
return this.lenght;
}

@Override
public void setLength(int len) {
this.lenght = len;
}

@Override
public int getSpeed() {
return this.speed;
}

@Override
public void setSpeed(int sp) {
this.speed = sp;
}

@Override
public void setSpeed(String sp) {
this.speed = Integer.parseInt(sp);
}

@Override
public String getType() {
return this.getClass().toString();
}

@Override
public void setType(String ty) {
//Please clarify what to set
//The above getClass will suffice the requirement
}

@Override
public String toString() {
return "P3 [numberEngines=" + numberEngines + ", lenght=" + lenght + ", speed=" + speed + "]";
}

  
}

TestClass.java

import java.util.ArrayList;
import java.util.List;

public class TestClass {

public static void main(String[] args) {
//2 Destroyers
Destroyer destroyer1 = new Destroyer(10, 1000, 100);
Destroyer destroyer2 = new Destroyer(0,0,0);
List<Destroyer> collectionOfDestroyers = new ArrayList<Destroyer>();
collectionOfDestroyers.add(destroyer1);
collectionOfDestroyers.add(destroyer2);

//2 Submarines
Submarine submarine1 = new Submarine(20, 2000, 200);
Submarine submarine2 = new Submarine(0, 0, 0);
List<Submarine> collectionOfSubmarine = new ArrayList<Submarine>();
collectionOfSubmarine.add(submarine1);
collectionOfSubmarine.add(submarine2);

List<Ship> collectionOfShips = new ArrayList<Ship>();
collectionOfShips.add(submarine1);
collectionOfShips.add(submarine2);
collectionOfShips.add(destroyer1);
collectionOfShips.add(destroyer2);

//2 P3s
P3 p3Obj1 = new P3(30, 3000, 300, 3);
P3 p3Obj2 = new P3(0, 0, 0, 2);

List<Contact> collectionOfContacts = new ArrayList<Contact>();
collectionOfContacts.add(destroyer1);
collectionOfContacts.add(destroyer2);
collectionOfContacts.add(submarine1);
collectionOfContacts.add(submarine2);
collectionOfContacts.add(p3Obj1);
collectionOfContacts.add(p3Obj2);
  
//Printing collections
  
collectionOfDestroyers.forEach(destroyer -> System.out.println(destroyer));System.out.println();
collectionOfSubmarine.forEach(submarine -> System.out.println(submarine));System.out.println();
collectionOfShips.forEach(ship -> System.out.println(ship));System.out.println();
collectionOfContacts.forEach(contact -> System.out.println(contact));System.out.println();
}

}