Java this piece of my code is not working .....I am trying to add Warship with a type or warship , but I keep getting an error ? //add another WarShip instance to the ArrayList. //use a foreach loop to process the ArrayList and print the data for each ship. //count the number of ships that are not afloat. Return this count to main for printing. //In the shipShow method: the number of ships that are still a Float public static int shipShow(ArrayList fleetList) { //from the ArrayList, remove the CargoShip that was declared as type Ship. fleetList.remove(2); //Removing ElFaro form the list // review Type casting instanceof | up casting Ship < Warship , subclass is a object of its parents //add warship USS John Warner - add cast by (Ship) new WarShip Ship warship2 = (Ship) new WarShip("USS John Warner", 2015, true, "attack submarine", "United States");   fleetList.add(warship2);   int count= 0; //use a foreach loop to process the ArrayList and print the data for each ship. for(Ship temp:fleetList) { //count the number of ships that are not afloat. Return this count to main for printing. if(temp.isAFloat()==false) { count++; //incrementing count if the ship is not aFloat } System.out.println(temp+"\n"); //displaying ship data } return count; //returning number of sinking ships }   } Full ShipTest Code  import java.util.ArrayList; import java.util.Arrays;   public class ShipTest {   public static void main(String[] args) { //make two objects with declared type Ship but actual type CruiseShip. Ship cruiseship1 = new CruiseShip("Magic",1998, true, 2700,"Caribbean"); // Super Class = ship SubClass = Cruiseship: Example of Up casting Ship cruiseship2 = new CruiseShip ("Titanic",1912, false, 1300, "Atlanic Ocean"); //make one object of declared type Ship but actual type CargoShip. Ship cargoship1 = new CargoShip("El faro",1974,false,"cargo", 391); //make one object of declared type CargoShip and actual type CargoShip. //casting needs to apply here because of the type Subtype = CargoShip | CargoShip CargoShip cargoship2 = new CargoShip ("Seawise Giant",1979,false,"crude oil", 564763); ///make a ship of declared type WarShip and actual type WarShip. Line 51 in BOLD , How do I add a Warship of Type Warship to my arraylist and Print this ? WarShip warship1 = new WarShip("USS Nimitz",1972,true,"suppcarrier ", "Unites States Navy"); //casting here //up casting is automatic // down casting is done manually //downcast the warship class by coding (Ship)cargo|warship to point the warship to the ship class // Array of type Ship named fleet = all the ships = 5 ships total ") Ship[]fleet = {cruiseship1,cruiseship2,cargoship1,cargoship2,(Ship) warship1}; //code a for loop that processes the fleet array to show display all ships and their data. for (Ship temp:fleet) {   System.out.println(temp);} //use a method of class Arrays to create an ArrayList of type Ship from the fleet array. //asList(fleet) class returns a list passed to the ArrayList construtor for creating an Array object ArrayListfleetlist = new ArrayList(Arrays.asList(fleet)); //pass this ArrayList to a method named shipShow that also displays all ships and returns an integer //report the number of ships can sank System.out.println(shipShow(fleetlist)+" of these ships sank!");   }   //In the shipShow method:     //add another WarShip instance to the ArrayList. //use a foreach loop to process the ArrayList and print the data for each ship. //count the number of ships that are not afloat. Return this count to main for printing. //In the shipShow method: the number of ships that are still a Float public static int shipShow(ArrayList fleetList) { //from the ArrayList, remove the CargoShip that was declared as type Ship. fleetList.remove(2); //Removing ElFaro form the list // review Type casting instanceof | up casting Ship < Warship , subclass is a object of its parents //add warship USS John Warner - add cast by (Ship) new WarShip Ship warship2 = (Ship) new WarShip("USS John Warner", 2015, true, "attack submarine", "United States");   fleetList.add(warship2);   int count= 0;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Java this piece of my code is not working .....I am trying to add Warship with a type or warship , but I keep getting an error ?

//add another WarShip instance to the ArrayList.

//use a foreach loop to process the ArrayList and print the data for each ship.

//count the number of ships that are not afloat. Return this count to main for printing.

//In the shipShow method: the number of ships that are still a Float

public static int shipShow(ArrayList<Ship> fleetList) {

//from the ArrayList, remove the CargoShip that was declared as type Ship.

fleetList.remove(2); //Removing ElFaro form the list

// review Type casting instanceof | up casting Ship < Warship , subclass is a object of its parents

//add warship USS John Warner - add cast by (Ship) new WarShip

Ship warship2 = (Ship) new WarShip("USS John Warner", 2015, true, "attack submarine", "United States");

 

fleetList.add(warship2);

 

int count= 0;

//use a foreach loop to process the ArrayList and print the data for each ship.

for(Ship temp:fleetList) {

//count the number of ships that are not afloat. Return this count to main for printing.

if(temp.isAFloat()==false) {

count++; //incrementing count if the ship is not aFloat

}

System.out.println(temp+"\n"); //displaying ship data

}

return count; //returning number of sinking ships

}

 

}

Full ShipTest Code 

import java.util.ArrayList;

import java.util.Arrays;

 

public class ShipTest {

 

public static void main(String[] args) {

//make two objects with declared type Ship but actual type CruiseShip.

Ship cruiseship1 = new CruiseShip("Magic",1998, true, 2700,"Caribbean");

// Super Class = ship SubClass = Cruiseship: Example of Up casting

Ship cruiseship2 = new CruiseShip ("Titanic",1912, false, 1300, "Atlanic Ocean");

//make one object of declared type Ship but actual type CargoShip.

Ship cargoship1 = new CargoShip("El faro",1974,false,"cargo", 391);

//make one object of declared type CargoShip and actual type CargoShip.

//casting needs to apply here because of the type Subtype = CargoShip | CargoShip

CargoShip cargoship2 = new CargoShip ("Seawise Giant",1979,false,"crude oil", 564763);

///make a ship of declared type WarShip and actual type WarShip.

Line 51 in BOLD , How do I add a Warship of Type Warship to my arraylist and Print this ?

WarShip warship1 = new WarShip("USS Nimitz",1972,true,"suppcarrier ", "Unites States Navy");

//casting here

//up casting is automatic

// down casting is done manually

//downcast the warship class by coding (Ship)cargo|warship to point the warship to the ship class

// Array of type Ship named fleet = all the ships = 5 ships total ")

Ship[]fleet = {cruiseship1,cruiseship2,cargoship1,cargoship2,(Ship) warship1};

//code a for loop that processes the fleet array to show display all ships and their data.

for (Ship temp:fleet) {

 

System.out.println(temp);}

//use a method of class Arrays to create an ArrayList of type Ship from the fleet array.

//asList(fleet) class returns a list passed to the ArrayList construtor for creating an Array object

ArrayList<Ship>fleetlist = new ArrayList<Ship>(Arrays.asList(fleet));

//pass this ArrayList to a method named shipShow that also displays all ships and returns an integer

//report the number of ships can sank

System.out.println(shipShow(fleetlist)+" of these ships sank!");

 

}

 

//In the shipShow method:

 

 

//add another WarShip instance to the ArrayList.

//use a foreach loop to process the ArrayList and print the data for each ship.

//count the number of ships that are not afloat. Return this count to main for printing.

//In the shipShow method: the number of ships that are still a Float

public static int shipShow(ArrayList<Ship> fleetList) {

//from the ArrayList, remove the CargoShip that was declared as type Ship.

fleetList.remove(2); //Removing ElFaro form the list

// review Type casting instanceof | up casting Ship < Warship , subclass is a object of its parents

//add warship USS John Warner - add cast by (Ship) new WarShip

Ship warship2 = (Ship) new WarShip("USS John Warner", 2015, true, "attack submarine", "United States");

 

fleetList.add(warship2);

 

int count= 0;

//use a foreach loop to process the ArrayList and print the data for each ship.

for(Ship temp:fleetList) {

//count the number of ships that are not afloat. Return this count to main for printing.

if(temp.isAFloat()==false) {

count++; //incrementing count if the ship is not aFloat

}

System.out.println(temp+"\n"); //displaying ship data

}

return count; //returning number of sinking ships

}

 

}

 

 

 

 

//report the value of the integer returned by shipShow.

//In the shipShow method:

//from the ArrayList, remove the CargoShip that was declared as type Ship.

//add another WarShip instance to the ArrayList.

//use a foreach loop to process the ArrayList and print the data for each ship.

//count the number of ships that are not afloat. Return this count to main for printing.

 

*25 Sniplile
jcruiseshipl, cruiseshipz, cargoshipi, cargoshipz, (ship) warshipi}N
26 //code a for loop that processes the fleet array to show display all ships and their data.
27
for (Ship temp: fleet) {
28
29
System.out.println (temp); }
30 //use a method of class Arrays to create an ArrayList of type Ship from the fleet array.
31
//asList (fleet) class returns a list passed to the ArrayList construtor for creating an Array object
ArrayList<Ship>fleetlist = new ArrayList<Ship> (Arrays.asList (fleet));
32
33
//pass this ArrayList to a method named shipShow that also displays all ships and returns an integer
//report the number of ships can sank
System.out.println (shipShow (fleetlist)+" of these ships sank!");
34
35
36
37
38
39
40
}
//In the shipShow method:
41
42 //add another WarShip instance to the ArrayList.
43 //use a foreach loop to process the ArrayList and print the data for each ship.
44 //count the number of ships that are not afloat. Return this count to main for printing.
45 //In the shipShow method: the number of ships that are still a Float
46 public static int shipShow (ArrayList<Ship> fleetList) {
47 //from the ArrayList, remove the CargoShip that was declared as type Ship.
48 fleetList.remove (2); //Removing ElFaro form the list
49 // review Type casting instanceof | up casting Ship < Warship, subclass is a object of its parents
50 //add warship USS John Warner add cast by (Ship) new WarShip
51 Ship warship2 = (Ship) new WarShip ("USS John Warner" 2015, true, "attack submarine" "United States");
52
53 fleetList.add (warship2);
54
¶
55 int count = 0;
56 //use a foreach loop to process the ArrayList and print the data for each ship.
57 for (Ship temp: fleetList) {
58 //count the number of ships that are not afloat. Return this count to main for printing.
Writable
Smart Insert
25:78: 1325
Transcribed Image Text:*25 Sniplile jcruiseshipl, cruiseshipz, cargoshipi, cargoshipz, (ship) warshipi}N 26 //code a for loop that processes the fleet array to show display all ships and their data. 27 for (Ship temp: fleet) { 28 29 System.out.println (temp); } 30 //use a method of class Arrays to create an ArrayList of type Ship from the fleet array. 31 //asList (fleet) class returns a list passed to the ArrayList construtor for creating an Array object ArrayList<Ship>fleetlist = new ArrayList<Ship> (Arrays.asList (fleet)); 32 33 //pass this ArrayList to a method named shipShow that also displays all ships and returns an integer //report the number of ships can sank System.out.println (shipShow (fleetlist)+" of these ships sank!"); 34 35 36 37 38 39 40 } //In the shipShow method: 41 42 //add another WarShip instance to the ArrayList. 43 //use a foreach loop to process the ArrayList and print the data for each ship. 44 //count the number of ships that are not afloat. Return this count to main for printing. 45 //In the shipShow method: the number of ships that are still a Float 46 public static int shipShow (ArrayList<Ship> fleetList) { 47 //from the ArrayList, remove the CargoShip that was declared as type Ship. 48 fleetList.remove (2); //Removing ElFaro form the list 49 // review Type casting instanceof | up casting Ship < Warship, subclass is a object of its parents 50 //add warship USS John Warner add cast by (Ship) new WarShip 51 Ship warship2 = (Ship) new WarShip ("USS John Warner" 2015, true, "attack submarine" "United States"); 52 53 fleetList.add (warship2); 54 ¶ 55 int count = 0; 56 //use a foreach loop to process the ArrayList and print the data for each ship. 57 for (Ship temp: fleetList) { 58 //count the number of ships that are not afloat. Return this count to main for printing. Writable Smart Insert 25:78: 1325
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education