Sale!

CSC/BIF 245 Assignment #4 Linked Lists, Interfaces Solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (8 votes)

Objects and Data Abstraction
Question 1 (30 points)

You are designing a program to manage animals in a farm. A Farm has a linked list of animals. An animal has the following characteristic: age, color, and gender. The farm has the following types of animals: cows, lambs, horses, and chicken. All animals must have method eat() but each of them implements it by displaying the amount of food that each animal consumes per day. A dog consumes up to 200g, a cow up to 5000 g, horses consume up to 7000 grams and chicken consume a max of 50g per day.

The program starts by displaying the following item:
1. Add an animal
2. Search for an animal
3. Sort
4. Remove by consumption
5. Exit
– – – – – – – – – — – – – –
Enter your choice:

If the user chooses 1 from the menu, the program prompts for information, creates an object of the appropriate type and adds it to the end of the list.

If the user chooses 2, the program prompts for the type and displays all animals of this particular type (for example, all horses).

If the user chooses 3, the program sorts all animals by the amount they eat and prints the sorted list. The actually list should be sorted.

If the user chooses 4, the program prompts for a value k and removes from the list all those animals that consume more than k grams per day.

If the user chooses 5, the program terminates.

Any other choice displays the menu again. If the user enters a choice outside the range [1,5], the menu is displayed again. If this is repeated more than 5 times, the program terminates.

Question 2 (10 points)
You are given the following class and interface. Use them and modify the code you wrote in answer to Question 1 so that you can sort your animals as described below. Redirect the sorted animals to a text file that you will name output.txt. In the code that I am giving you, do not modify the methods sort() and show() in class TestProgram nor interface Sortable.

import java.util.*;

public class TestProgram
{
final static int SIZE=5;

public static void sort(Sortable[] items)
{
/*sorts an array of items in an increasing order as specified in the compareTo() method*/

for (int i = 1; i < items.length; i++ ) { Sortable key = items[i]; int position = i; while (position > 0 && items[position-1].compareTo(key) > 0)
{
items[position]=items[position-1];
position–;
}
items[position]=key;
}
}

public static void show(Sortable[] items)
{
for (int i = 0; i < items.length; i++ )
{
if (items[i]!= null)
System.out.print(items[i]);
}
}

public static void main (String[] args)
{
Animal animals= new Animal[SIZE];

/* enter here code to read information from a file that you will name ”input.txt”. Each line in the file specifies an object. The first word on each line specifies the type of the object (horse, lamb, chicken, etc.). Then, the remaining tokens on the line specify the features/characteristics of the animal. Have the code create the objects and add them to the array animals.*/

sort(animals);
show(animals);
}
}

public interface Sortable
{
public int compareTo(Object o);
/* a.compareTo(b) returns 0 if a is equal to b, a number greater than 0 if a is greater than b and a number less than 0 if a is less than b. Animals are compared by age.*/
}

Plagiarism and cheating policy

Cheating is a serious offense. If a student is found to have copied part or all of the assignment, he or she will receive a zero on the assignment. During exams, if a student is caught cheating, he or she will receive a zero on the exam. This might result in the student getting an F on the course. NO EXCUSES WILL BE ACCEPTED. The same applies to the person providing others with material. The same applies to plagiarism (presenting someone else’s work as being one’s own work).

Deadline policy

Deadlines for submitting assignments will be announced in class or on the assignments. They are firm. Students must respect them. A student failing to meet the deadline will get 5% of the grade deducted per day late. A student cannot replace an assignment with another one. If he or she fails to submit an assignment, a zero will be scored on the missed assignment. To illustrate, if the student submits assignments 1 and 3 only, he or she will get a 0 on Assignment 2, Assignment 1 or 3 cannot compensate for the missed assignment.