JAVA PROGRAMMING •The owner of Sara’s Vet Clinic likes to track information abou
ID: 3676602 • Letter: J
Question
JAVA PROGRAMMING
•The owner of Sara’s Vet Clinic likes to track information about her patients and do analysis. She would like to enter information about the animals that she treats on a given day. She would like to enter the, Animal type ( dog, cat or snake) and the Animal weight.
•To help with planning the most appointments she can have in a day (and thus would need to enter) is 30.
She would like a report printed to the screen listing the mean of all animals entered for the day. The median of all animals entered and the mean by animal type entered. An example report is
Mean weight of all animals is 20.25
Median weight of all animals is 21
Mean weight of dogs is 35.3
Mean weight of cats is 10.5
Mean weight of snakes is 2.25
•Create, run and then debug a program to meet the given requirements
•While you do not need to turn in a UML diagram or an IPO chart with this lab, I HIGHLY recommend sketching one out on a sheet of paper before you begin.
•You do not need to do user data validation in this lab, simply meet the stated requirements.
•You must properly decompose the problem.
Explanation / Answer
import java.util.*;
import java.util.Scanner;
import java.util.Collections;
public class Animals {
public static void main(String[] args) {
List<Double> total = new ArrayList<Double>();
List<Double> dog = new ArrayList<Double>();
List<Double> cat = new ArrayList<Double>();
List<Double> snake = new ArrayList<Double>();
String fooString1 = new String("dog");
String fooString2 = new String("cat");
String fooString3 = new String("snake");
Scanner stdin = new Scanner(System.in);
do {
String type;
System.out.println("Enter the type of Animal: ");
type = stdin.next();
System.out.println("Enter the weight of Animal: ");
double weight = stdin.nextDouble();
if(type.equals(fooString1)) dog.add(weight);
else if(type.equals(fooString2)) cat.add(weight);
else if (type.equals(fooString3)) snake.add(weight);
total.add(weight);
System.out.println("Add more? (y/n)");
if (stdin.next().startsWith("n")) break;
} while (true);
System.out.println("Mean weight of all animals is " + calculateMean(total));
System.out.println("Median weight of all animals is " + calculateMedian(total));
System.out.println("Mean weight of dogs is " + calculateMean(dog));
System.out.println("Mean weight of cats is " + calculateMean(cat));
System.out.println("Mean weight of snakes is " + calculateMean(snake));
}
public static double calculateMean(List<Double> arr)
{
Double sum = 0.0;
// Sum all values in our array
for (int i=0; i<arr.size(); i++)
{
sum = sum + arr.get(i);
}
// Calculate mean
double mean = ((double) sum) / ((double) arr.size());
return mean;
}
public static double calculateMedian(List<Double> arr)
{
boolean performedSwap = true;
int tempValue = 0;
// Sort our array
while(performedSwap)
{
performedSwap = false;
// Iterate through the array, swapping pairs that are out of order.
// If we performed a swap, we set the "performedSwap" flag to true
for (int i=0; i < arr.size()-1 ; i++)
{
if (arr.get(i) > arr.get(i+1))
{
Collections.swap(arr, i, i+1);
performedSwap = true;
}
}
}
double median = 0;
// If our array's length is even, then we need to find the average of the two centered values
if (arr.size() % 2 == 0)
{
int indexA = (arr.size() - 1) / 2;
int indexB = arr.size() / 2;
median = ((double) (arr.get(indexA) + arr.get(indexB))) / 2;
}
// Else if our array's length is odd, then we simply find the value at the center index
else
{
int index = (arr.size() - 1) / 2;
median = arr.get( index );
}
return median;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.