Has to be in Java (Bluejay) 1) Polish the class that you proposed as your proble
ID: 3882749 • Letter: H
Question
Has to be in Java (Bluejay)
1) Polish the class that you proposed as your problem statement.
(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
Solution 1:
"Polish the class that you proposed as your problem statement."
Please provide the class to be improved ( or polish)
Solution 2:
Please find the implementation:
Assumptions: Since here in the problem statement 'Person' is held as example so a default implementation with some data members is used to complete the Tester class of Q2.
PersonSample.java [Person]
import java.util.Comparator;
public class PersonSample {
private String name;
private int age;
private double height;
//Parameterized Constructor
public PersonSample(String name, int age, double height) {
super();
this.name = name;
this.age = age;
this.height = height;
}
//Respective getter() and setter()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//hasCode() and equals() for comparing objects for eg: search
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonSample other = (PersonSample) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//Comparators for sorting on the given criterion
static Comparator<PersonSample> nameComparator = Comparator.comparing(PersonSample :: getName);
static Comparator<PersonSample> ageComparator = Comparator.comparing(PersonSample :: getAge);
static Comparator<PersonSample> heightComparator = Comparator.comparing(PersonSample :: getHeight);
@Override
public String toString() {
return "PersonSample [name=" + name + ", age=" + age + ", height=" + height + "]";
}
}
ArrayOptions.java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayOptions {
static PersonSample[] personArray = new PersonSample[10];
public static PersonSample takeUserInput(){
System.out.println("Enter details of person in the following sequence [Name, Age, Height]: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
double height = scanner.nextDouble();
return new PersonSample(name, age, height);
}
public static void choice(){
System.out.println("1.Search 2.Replace with given index 3.Modify 4.Sort with Name OR Age OR Height 5.Exit");
//ask input from user
System.out.println("Enter your choice: ");
Scanner inputScanner=new Scanner(System.in);
int inputChoice=inputScanner.nextInt();
int check=inputChoice;
//User will be asked for choice until chosen for Exit
while (inputChoice!=5){
System.out.println("1.Search 2.Replace with given index 3.Modify 4.Sort with Name OR Age OR Height 5.Exit");
int exitChoice=inputScanner.nextInt();
check=exitChoice;
}
switch (check){
case 1 :
System.out.println("Pleas enter name to search");
search(inputScanner.nextLine());
break;
case 2 :
System.out.println("Please enter the index");
int index = inputScanner.nextInt();
replace(index, takeUserInput());
break;
case 3 :
modify();
break;
case 4 :
System.out.println("Choose one criteria to sort among Name OR Age Height : ");
String choiceOfSortingCriteria = inputScanner.nextLine();
sort(choiceOfSortingCriteria);
break;
case 5 :
System.exit(0);
break;
}
}
//Method to search
public static void search(String name){
for(int i=0; i<personArray.length;i++){
int age=0;double height=0f;
if(personArray[i].equals(new PersonSample(name,age, height))){
System.out.println("Found : " + personArray[i].toString());
}else{
System.out.println("Not Found");
}
}
}
//Method to replace
public static void replace(int index, PersonSample personObject){
if(index > personArray.length){
System.out.println("Please enter valid index");
}
personArray[index] = personObject;
}
//Method to modify
public static void modify(){
System.out.println("Choose from the objects listed to modify:");
for(int i=0;i<personArray.length;i++){
System.out.println(personArray.toString());
}
System.out.println("Select index [1-10]:");
Scanner scanner = new Scanner(System.in);
int indexSelected = scanner.nextInt();
personArray[indexSelected] = takeUserInput();;
}
//Method to sort
public static void sort(String criterion){
List<PersonSample> buffList = Arrays.asList(personArray);
if(criterion.equalsIgnoreCase("name"))
Collections.sort(buffList, PersonSample.nameComparator);
else if(criterion.equalsIgnoreCase("age"))
Collections.sort(buffList, PersonSample.ageComparator);
else if(criterion.equalsIgnoreCase("height"))
Collections.sort(buffList, PersonSample.heightComparator);
System.out.println("Sorted list: ");
buffList.forEach(person -> System.out.println(person.toString()));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Taking 10 user inputs
for(int i = 0; i< 10; i++){
personArray[i] = takeUserInput();
}
choice();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.