
Concept explainers
I have the following code in java:
import java.util.ArrayList;
import java.util.Scanner;
public class Auxiliar {
static ArrayList<Animal> animales = new ArrayList<>();
static void imprimirAnimales() {
for (Animal animal : animales) {
System.out.println("Nombre: " + animal.getNombre() + ", Especie: " + animal.getEspecie() + ", Edad: " + animal.getEdad());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int opcion = 0;
while (opcion != 5) {
System.out.println("¿Qué acción desea realizar?");
System.out.println("1. Añadir animal");
System.out.println("2. Modificar animal");
System.out.println("3. Eliminar animal");
System.out.println("4. Imprimir lista de animales");
System.out.println("5. Salir");
opcion = scanner.nextInt();
scanner.nextLine();
switch (opcion) {
case 1:
agregarAnimal(scanner);
break;
case 2:
modificarAnimal(scanner);
break;
case 3:
eliminarAnimal(scanner);
break;
case 4:
imprimirAnimales();
break;
case 5:
break;
default:
System.out.println("Opción inválida");
break;
}
}
}
static void agregarAnimal(Scanner scanner) {
System.out.println("Ingrese el nombre del animal:");
String nombre = scanner.nextLine();
System.out.println("Ingrese la especie del animal:");
String especie = scanner.nextLine();
System.out.println("Ingrese la edad del animal:");
int edad = scanner.nextInt();
scanner.nextLine();
Animal animal = new Animal(nombre, especie, edad);
animales.add(animal);
System.out.println("Animal agregado correctamente.");
}
static void modificarAnimal(Scanner scanner) {
System.out.println("Ingrese el índice del animal a modificar:");
int indice = scanner.nextInt();
scanner.nextLine();
if (indice >= 0 && indice < animales.size()) {
Animal animal = animales.get(indice);
System.out.println("Ingrese el nuevo nombre del animal:");
String nombre = scanner.nextLine();
System.out.println("Ingrese la nueva especie del animal:");
String especie = scanner.nextLine();
System.out.println("Ingrese la nueva edad del animal:");
String edad = scanner.next();
animal.setNombre(nombre);
animal.setEspecie(especie);
animal.setEdad(Integer.parseInt(edad));
System.out.println("Animal modificado correctamente.");
} else {
System.out.println("Índice inválido.");
}
}
static void eliminarAnimal(Scanner scanner) {
System.out.println("Ingrese el índice del animal a eliminar:");
int indice = scanner.nextInt();
scanner.nextLine();
if (indice >= 0 && indice < animales.size()) {
animales.remove(indice);
System.out.println("Animal eliminado correctamente.");
} else {
System.out.println("Índice inválido.");
}
}
}
class Animal {
private String nombre;
private String especie;
private int edad;
public Animal(String nombre, String especie, int edad) {
this.nombre = nombre;
this.especie = especie;
this.edad = edad;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getEspecie() {
return especie;
}
public void setEspecie(String especie){
this.especie = especie;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
}
And I want to make the following changes to it:
1. The input asks for the name, species and age of the animal. Change this data to be: Name, code, sex, age, state and food.
2. If a value other than a number is added when requesting the age, the program generates an error. Can it be returned to be entered again in number format?
3. Once the whole process is finished (using option 5 to cancel) the list of all registered animals is printed

Step by stepSolved in 3 steps with 4 images

Hello! I'm not sure if it shows the second answer I got for the question where they had to be modified to invoke a list of lists.
The code worked perfectly for me, but the "delete animals" option does not make any changes to the list and it stays as it is. What I can do?

From the code supplied by the tutor, modify it so that:
1. Ask what kind of animal it is (for example, carnivore, herbivore, and omnivore) When prompting for the information, it is saved in a list for each of the three animal types (for example, lions in carnivores, bears in omnivores, giraffes in herbivores) and they are saved depending on the number that the user adds in each list.
2. At the end, a list of lists is printed that presents the data of each of the saved animals according to their type.
Hello! I'm not sure if it shows the second answer I got for the question where they had to be modified to invoke a list of lists.
The code worked perfectly for me, but the "delete animals" option does not make any changes to the list and it stays as it is. What I can do?

From the code supplied by the tutor, modify it so that:
1. Ask what kind of animal it is (for example, carnivore, herbivore, and omnivore) When prompting for the information, it is saved in a list for each of the three animal types (for example, lions in carnivores, bears in omnivores, giraffes in herbivores) and they are saved depending on the number that the user adds in each list.
2. At the end, a list of lists is printed that presents the data of each of the saved animals according to their type.
- 1 import java.util.Random; 2 3 4 5 6- 7 8 9 10 11 12 ▼ import java.util.StringJoiner; public class preLabC { public static MathVector myMethod(int[] testValues) { // Create an object of type "MathVector". // return the Math Vector Object back to the testing script. return VectorObject; 13 14 15 } } ▸ Comments Description The MathVector.java file contains a class (Math Vector). This class allows you to create objects in your program that can contain vectors (a 1D array) and perform actions on those vectors. This is useful for doing math operations like you would in your linear algebra class. Your method should evaluate against two tests. What do you need to do in this exercise? You need to create an "instance" of the MathVector object, VectorObject, in your method. If you do this correctly, your method will accept a 1D array of integers, testValues, feed it into the VectorObject and then return the VectorObject. If you look in the file MathVector.java you'll see that this is one way in…arrow_forwardWarmup Write a method called countSuit) that takes an array of Cards and a String suit as parameters and counts how many of the cards have the given suit. Complete the definition of the class Warmup and method definition countSuit shown below. public class Warmup { public static int countSuit(Card [] cards, String suit) { // fill in code here } } For example, if we ran the following code in the main method: Card[] cards = new Card[4]; cards [0] = new Card (4, "clubs"); cards [1] = new Card (12, "diamonds"); cards [2] = new Card (1, "hearts"); cards [3] new Card(10, "diamonds"); System.out.println(countSuit(cards, "diamonds")); the output printed would be 2.arrow_forwardimport java.util.Scanner;import java.util.ArrayList;import java.util.StringTokenizer; public class PlantArrayListExample { // TODO: Define a printArrayList method that prints an ArrayList of plant (or flower) objects public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String input; // TODO: Declare an ArrayList called myGarden that can hold object of type plant // TODO: Declare variables - plantName, plantCost, flowerName, flowerCost, colorOfFlowers, isAnnual input = scnr.next(); while(!input.equals("-1")){ // TODO: Check if input is a plant or flower // Store as a plant object or flower object // Add to the ArrayList myGarden input = scnr.next(); } // TODO: Call the method printArrayList to print myGarden }}arrow_forward
- Add the method below to the parking office.java class. Method getParkingCharges(ParkingPermit) : Money Parkingoffice.java public class ParkingOffice {String name;String address;String phone; List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();}public Customer register() {Customer cust = new Customer(name,address,phone);customers.add(cust);return cust;}public Car register(Customer c,String licence, CarType t) {Car car = new Car(c,licence,t);cars.add(car);return car;}public Customer getCustomer(String name) {for(Customer cust : customers)if(cust.getName().equals(name))return cust;return null;}public double addCharge(ParkingCharge p) {charges.add(p);return p.amount;} public String[] getCustomerIds(){String[] stringArray1 = new String[2];for(int…arrow_forwarduse javaarrow_forwardAdd the method below to the parking office.java class. Method getParkingCharges(Customer) : Money Parkingoffice.java public class ParkingOffice {String name;String address;String phone; List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();}public Customer register() {Customer cust = new Customer(name,address,phone);customers.add(cust);return cust;}public Car register(Customer c,String licence, CarType t) {Car car = new Car(c,licence,t);cars.add(car);return car;}public Customer getCustomer(String name) {for(Customer cust : customers)if(cust.getName().equals(name))return cust;return null;}public double addCharge(ParkingCharge p) {charges.add(p);return p.amount;} public String[] getCustomerIds(){String[] stringArray1 = new String[2];for(int i=0;i<2;i++){stringArray1[i]…arrow_forward
- package edu.umsl.iterator;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;public class Main {public static void main(String[] args) {String[] cities = {"New York", "Atlanta", "Dallas", "Madison"};Collection<String> stringCollection = new ArrayList<>(Arrays.asList(cities));Iterator<String> iterator = stringCollection.iterator();while (iterator.hasNext()) {System.out.println(/* Fill in here */);}}} Rewrite the while loop to print out the collection using an iterator. Group of answer choices iterator.toString() iterator.getClass(java.lang.String) iterator.remove() iterator.next()arrow_forwardUse this exercise to test your code that you wrote in the previous exercise. CookBook Class In this exercise, you are going to complete the CookBook class. public class CookBook { private ArrayList<Recipe> recipes; private String name; public CookBook(String name){ this.name = name; recipes = new ArrayList<Recipe>(); } /** * This method adds a recipe to the ArrayList of Recipes * @param recipe a recipe object to be added to the list */ public void addRecipe(Recipe recipe){ /* Implement this method */ } /** * This method removes all recipes from the * ArrayList of Recipes where the title matches the input * string * * @param title the title of the recipe to remove * @return the number of recipes removed */ public int removeRecipe(String title){ /* Implement this method */ } /** * This method lists the title of all the recipes in the * cook book * */ public void listAll(){ /* Implement this method */ } } The addRecipe method should take a recipe and add it to the end of the…arrow_forwardimport java.util.*; public class Main{ public static void main(String[] args) { Main m = new Main(); m.go(); } private void go() { List<Stadium> parks = new ArrayList<Stadium>(); parks.add(new Stadium("PNC Park", "Pittsburgh", 38362, true)); parks.add(new Stadium("Dodgers Stadium", "Los Angeles", 56000, true)); parks.add(new Stadium("Citizens Bank Park", "Philadelphia", 43035, false)); parks.add(new Stadium("Coors Field", "Denver", 50398, true)); parks.add(new Stadium("Yankee Stadium", "New York", 54251, false)); parks.add(new Stadium("AT&T Park", "San Francisco", 41915, true)); parks.add(new Stadium("Citi Field", "New York", 41922, false)); parks.add(new Stadium("Angels Stadium", "Los Angeles", 45050, true)); Collections.sort(parks, Stadium.ByKidZoneCityName.getInstance()); for (Stadium s : parks) System.out.println(s); }}…arrow_forward
- Create a fee invoice application for students attending Valence College, a college in the State of Florida. There are two types of students: graduate and undergraduate. Out-of-state undergraduate students pay twice the tuition of Florida-resident undergraduate students (all students pay the same health and id fees). A graduate student is either a PhD or a MS student. PhD students don't take any courses, but each has an advisor and a research subject. Each Ms and Phd student must be a teaching assistant for one undergraduate course. MS students can only take graduate courses. A course with a Course Number (crn) less than 5000 is considered an undergraduate course, and courses with a 5000 crn or higher are graduate courses. CRN Course Credit Hours. 4587 MAT 236 4 4599 COP 220 3 8997 GOL 124 1 9696 COP 100 3 4580 MAT 136 1 2599 COP 260 3 1997 CAP 424 1 5696 KOL 110 2 7587 MAT 936 5 2599 COP 111 3 6997 GOL 109 1 2696 COP 101 3 5580 MAT 636 2 2099 COP 268 3 4997 CAP 427 1 3696 KOL 910 2…arrow_forwardPROBLEM STATEMENT: In this problem you will need to insert 5 at the frontof the provided ArrayList and return the list. import java.util.ArrayList;public class InsertElementArrayList{public static ArrayList<Integer> solution(ArrayList<Integer> list){// ↓↓↓↓ your code goes here ↓↓↓↓return new ArrayList<>(); Can you help me with this question The Language is Javaarrow_forwardimport java.util.Scanner; public class Inventory { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); InventoryNode headNode; InventoryNode currNode; InventoryNode lastNode; String item; int numberOfItems; int i; // Front of nodes list headNode = new InventoryNode(); lastNode = headNode; int input = scnr.nextInt(); for(i = 0; i < input; i++ ) { item = scnr.next(); numberOfItems = scnr.nextInt(); currNode = new InventoryNode(item, numberOfItems); currNode.insertAtFront(headNode, currNode); lastNode = currNode; } // Print linked list currNode = headNode.getNext(); while (currNode != null) { currNode.printNodeData(); currNode…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





