Has to be in Java (Bluejay) 1) Polish the class that you proposed as your proble
ID: 3882798 • Letter: H
Question
Has to be in Java (Bluejay)
1) Polish the class that you proposed as your problem statement. class is Animals
(1.5 points) at least 3 instance variables of different types (e.g., String, int, double, boolean, etc).
(1.5 points) a default constructor that is composed of a sequence of this.set() methods.
(1.5 points) a custom constructor that is composed of a sequence of this.set() methods.
(1.5 points) a get() method for each instance variable
(1.5 points) a set() method for each instance variable. Each set() method uses the this keyword.
(1.5 points) a toString() method
2) Write a Tester for your class that sets up a data set each of whose elements is of your class’ type. Your tester must meet the following requirements
(1 point) Instantiates an array of a certain size – e.g, 10 elements (like in the reference code)
(2 points) Takes user input to populate the array
(2 points) Uses a do-while loop and a switch statement (like we did in a previous lab) to ask the user what they want to do with the data
(6 points @ 1.5 points each) Has at least four options that will correctly handle the user’s selection when called.
Option 1: Search the array for objects that meet a search criterion. Display the objects if found.
Option 2: Replace an object at a given index with another object.
Option 3: Allow the user to select an element from the array and then modify the element (e.g., select a person from the array of persons and then sets the name of that person to a new value).
Option 4: Sort the array of objects according to a given criterion (e.g., sort the array of persons by height).
Option 5: Exit the program.
Explanation / Answer
package snippet;
import java.util.Scanner;
public class TestAnimal {
public static void main(String args[]) {
Animal animals[] = new Animal[10];
String name;
String voice;
int legs;
Scanner sc = new Scanner(System.in);
for(int i=0;i<animals.length;i++) {
System.out.print("Enter animal name ");
name = sc.next();
System.out.println();
System.out.print("Enter animal voice");
voice = sc.next();
System.out.println();
System.out.print("Enter animal legs ");
legs = sc.nextInt();
animals[i] = new Animal(name, voice, legs);
}
int choice;
do {
System.out.println();
System.out.print("Enter choice ");
choice = sc.nextInt();
if(choice == 1) {
int flag = 0;
System.out.println("Enter animal details to be search");
System.out.println();
System.out.print("Enter name");
name = sc.next();
System.out.println();
System.out.print("Enter voice");
voice = sc.next();
System.out.println();
System.out.print("Enter legs");
legs = sc.nextInt();
for(int i=0;i<animals.length;i++) {
Animal animal = animals[i];
if(animal.getName().equals(name) && animal.getNoLegs() == legs && animal.getVoice().equals(voice)) {
System.out.println("Animal found");
flag = 1;
}
}
if(flag == 0) {
System.out.println("No such animal found");
}
}else if(choice == 2) {
int index = 0;
System.out.println("Enter index for which the data is to be changed");
index = sc.nextInt();
System.out.print("Enter name");
name = sc.next();
System.out.println();
System.out.print("Enter voice");
voice = sc.next();
System.out.println();
System.out.print("Enter legs");
legs = sc.nextInt();
animals[index] = new Animal(name, voice, legs);
}else if(choice == 3) {
int index = 0;
System.out.println("Enter index for which the data is to be changed");
index = sc.nextInt();
System.out.print("Enter name");
name = sc.next();
animals[index].setName(name);
}else if(choice == 4) {
}else
{
break;
}
}while(true);
}
}
class Animal{
String name;
String voice;
int noLegs;
public String toString() {
return this.getName() +" "+this.getNoLegs()+" legs and makes "+this.getVoice()+" voice";
}
public Animal() {
this.setName(null);
this.setNoLegs(0);
this.setVoice(null);
}
public Animal(String n, String v, int l) {
this.setName(n);
this.setNoLegs(l);
this.setVoice(v);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVoice() {
return voice;
}
public void setVoice(String voice) {
this.voice = voice;
}
public int getNoLegs() {
return noLegs;
}
public void setNoLegs(int noLegs) {
this.noLegs = noLegs;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.