Question

in Java and make sure the extra credit is in the program aswell

Submission Instructions Submit by midnight on the due date and include your submission (a zip file) as an attachment. Assignm

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code-

import java.util.*;
class Animal{
int age,amount;
String name,food;
//toString method to convert
public String toString(){
return "Name : "+name+" Age : "+age+" Amount : "+amount+" Food : "+food;
}
//NO ARGUEMENT CONSTRUCTOR
Animal(){
name = "unknown";
age = 0;
amount = 0;
food = "none";
}
}
public class Main
{
   public static void main(String[] args) {
  
   //ANIMAL NAME
   String[] names = {"Rhino","Deer","Snake","Elephant","Cat"};
   //ANIMAL FOOD
   String[] foods = {"Grass","Oats","Fish","Meat"};
   Random r = new Random();
   //15 ANIMAL Object
Animal obj[] = new Animal[15];
int i;
//INITALZIE OBJECT TO NULL
for(i = 0 ; i<15;i++){
obj[i] = new Animal();
}
//generate random value for animal object
for( i = 0 ;i<15;i++){
obj[i].name = names[r.nextInt(5)];
obj[i].age = r.nextInt(13)+2;
obj[i].amount = r.nextInt(45)+5;
obj[i].food = foods[r.nextInt(3)];
}
//sort the animal object according to age
int n = obj.length;
for (i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (obj[j].age > obj[j+1].age)
{
// swap temp and arr[i]
Animal temp = obj[j];
obj[j] = obj[j+1];
obj[j+1] = temp;
}
//calculate the cost of each animal
int rhinoAmount=0, deerAmount=0,snakeAmount=0,elephanAmount=0,catAmount=0;
for( i = 0 ;i<15;i++){
if(obj[i].name=="Rhino")
rhinoAmount+=obj[i].amount*foodType(obj[i]);
if(obj[i].name=="Deer")
deerAmount+=obj[i].amount*foodType(obj[i]);
if(obj[i].name=="Snake")
snakeAmount+=obj[i].amount*foodType(obj[i]);
if(obj[i].name=="Elephant")
elephanAmount+=obj[i].amount*foodType(obj[i]);
if(obj[i].name=="Cat")
catAmount+=obj[i].amount*foodType(obj[i]);
  
}
//print the animal object in sort according to age
for(i =0;i<15;i++){
System.out.println(obj[i].toString());
}
System.out.println("The total cost to feed Rhino per day is $"+rhinoAmount);
System.out.println("The total cost to feed Deer per day is $"+deerAmount);
System.out.println("The total cost to feed Snake per day is $"+snakeAmount);
System.out.println("The total cost to feed Elephant per day is $"+elephanAmount);
System.out.println("The total cost to feed Cat per day is $"+catAmount);
  

   }
   static int foodType(Animal a){
   if(a.food=="Grass")
   return 1;
   if(a.food=="Oats")
   return 2;
   if(a.food=="Fish")
   return 5;
   if(a.food=="Meat")
   return 8;
   else
   return 0;
   }
}

Screenshots -

: : : : © Computer Science question I ch + Online Java Compiler-online ed + x a https://www.onlinegdb.com/online java compile

Add a comment
Know the answer?
Add Answer to:
In Java and make sure the extra credit is in the program aswell
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JavaScript - Sorting Assignment (arrays, classes, functions and higher order functions) Note: Please make sure to...

    JavaScript - Sorting Assignment (arrays, classes, functions and higher order functions) Note: Please make sure to use the ES6 keyword style => instead of the older function. For variables please use keyword let, not var, class and within the constructor, not function. Instructions: - Create a new JavaScript file and name it “Objects.js” - Create a class and make sure to use “names” as the identifier. - Create a constructor with the following elements: first ( value passed will be...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • The goal of this homework is to write a small database system for an Animal Shelter....

    The goal of this homework is to write a small database system for an Animal Shelter. This is a no-kill shelter like the one just west of the Addition Airport. You will accept animal “donations” to the shelter, but mostly only dogs and cats. (But yes, there may be the occasional hamster or other nondog, non-cat animal donation.) At present, all we need to do is write the skeleton of a database that will track all the animals in the...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Write a program in a class SandwichCounter that computes the cost of sandwiches sold at a...

    Write a program in a class SandwichCounter that computes the cost of sandwiches sold at a food stand. Three kinds of sandwiches —cheese steak, chicken, and salad— are cost, respectively, $8.5, $7.5, and $7.0 per sandwich. Create an array of strings that holds the names of these sandwiches. Create another array that holds the cost of each corresponding sandwich. Your program should read the name of a sandwich and the quantity desired by a customer. Locate the sandwich in the...

  • Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class...

    Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class has  different constructors the first without parameters ,the  second with three parameters and the third with two parameters. In the main method  create three objects using thee different constructors to initialize the Variables.  then print out all the information a bout all objects .

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT