Uploaded by hitoshily123

CS 46A Homework 9

advertisement
Computer Science 46A
SJSU Computer Science
CS 46A Homework 9
Overview
In this assignment, you’ll demonstrate your ability to write classes that …
Learning Outcomes
By the end of this assignment, you should be able to write a Java program to …
• … create ArrayLists for different types of objects.
• … write classes that operate on ArrayLists.
Guidelines
1. Use BlueJ to create your code.
2. You must name your classes exactly as specified. Otherwise Codecheck will not be able
to process your submission and you will get no credit.
3. When you are finished with your code, submit it to Codecheck one final time then
download the .signed.zip file.
4. You must upload all three signed.zip files together to Canvas and you should double
check the files in Canvas to make sure all three zip files are uploaded.
5. Do not open the downloaded zip files. The files are digitally signed, and the grader
program will check that they have not been opened.
Homework 9
Page 1 of 4
Computer Science 46A
SJSU Computer Science
Problem 9A
Codecheck Link: HERE
Goal:
In this problem, you will write a class called FrogList that will manage an array list of class Frog
(provided) that has attributes for a frog’s weight and the number of legs it has (frogs start with no legs
as tadpoles, and grow legs as they age).
An example of the ArrayList is as follows:
[Frog[Weight:10,Legs:2], Frog[Weight:1,Legs:0]]
Instructions:
Start a new BlueJ project called hw9a in the cs46a/homework/hw09 folder. In the BlueJ project, create
two classes called Frog and FrogListTester. Next, create a class called FrogList (there is no
starter code provided for this example). Complete your program with the following items:
1. A constructor:
public FrogList()
Constructs a new FrogList object that initialized the list to an empty list
2. Four methods:
public void add(Frog frog)
Adds a frog to the beginning of the list.
public Frog get(int index)
Returns the Frog object at position index if the index is valid. If the index is invalid, then
return null.
public int countInRange(int lowLimit, int highLimit)
Gets the number of frogs in the list that have a weight in the range between lowLimit and
highLimit (inclusive).
public String toString()
Returns the string returned from calling the toString() method on the list.
Tips and Guidelines:
• Use an enhanced for loop for the countInRange() method.
• Javadocs is required on the class FrogList
Homework 9
Page 2 of 4
Computer Science 46A
SJSU Computer Science
Problem 9B
Codecheck Link: HERE
Goal:
In this problem, you will write a class called StockList that will manage an array list of class Stock
(provided).
An example of the array list is as follows:
[[Stock[symbol=FB,price=276.78], Stock[symbol=MSFT,price=224.15],
Stock[symbol=AMZN,price=3322.0], Stock[symbol=GOOGL,price=1757.76]]
Instructions:
Start a new BlueJ project called hw9b in the cs46a/homework/hw09 folder. In the BlueJ project, create
two classes called Stock and StockListTester. Next, create a class called StockList (there is no
starter code provided for this example). Complete your program with the following items:
1. A constructor:
public StockList()
Constructs a new StockList object that initializes the instance variable to an empty list
2. Four methods:
public void add(Stock s)
Adds a stock to the end of the list.
public void swap(int index1, int index2)
Swap the element at index1 with the element at index2. If either index is out of bounds, do
not change anything.
public String cheapest()
Gets the symbol for the Stock with the lowest price per share. If more than one Stock has
the same price, return the symbol for the first. If the list is empty, then return null.
public String toString()
Returns the string returned from calling the toString() method on the list.
Tips and Guidelines:
• Use an enhanced for loop for the cheapest() method.
• Use only one temporary variable for the swap() method.
• Javadocs is required on the class StockList
Homework 9
Page 3 of 4
Computer Science 46A
SJSU Computer Science
Problem 9C
Codecheck Link: HERE
Goal:
In this problem, you will write a class called PeopleListApp that will manage an array list of class
People (provided) that has attributes for a person’s initials and their age.
An example output of the PeopleListApp class is as follows:
Enter the number of people: 4
Enter an integer as the random generator seed: 3
All people before swapping:
Person[initials=SM,age=52]
Person[initials=HQ,age=91]
Person[initials=VG,age=47]
Person[initials=XP,age=83]
All people after swapping:
Person[initials=XP,age=83]
Person[initials=HQ,age=91]
Person[initials=VG,age=47]
Person[initials=SM,age=52]
Instructions:
In the BlueJ project, create a classes called Person and PeopleListApp and copy over the code from
Canvas. Next, follow the steps in the PeopleListApp class to complete this problem.
Homework 9
Page 4 of 4
Download