Question: PROBLEM 1 Arrays (Note: There are two separate problems in this HW.) Write a Java application to simulate coin tosses and compute the fraction of

PROBLEM 1 Arrays (Note: There are two separate problems in this HW.)

Write a Java application to simulate coin tosses and compute the fraction of heads and fraction of tails. As a challenge, determine the longest run of one outcome; that is, the most consecutive heads or consecutive tails (which ever is longer).

To simulate the toss of a coin, use the random method in the Math utility class, that generates a number greater than or equal to 0 and less than 1.0. The notation for this range is [0 , 1.0), where the square bracket means to include the 0 in the range and the rounded parenthesis means to exclude the 1.0 from the range. Since there are two outcomes of equal probability in the toss of a fair coin, divide the range of numbers between [0 ,1.0) into two roughly equal size sub-ranges. Assume any number less than 0.5 will be considered as a tail while any number greater than or equal to 0.5 as a head.

Program Organization Requirements:

CoinToss.java

A parameterized constructor that accepts an integer parameter for the number of tosses. This value will be used to set the size of an array that will hold the coin toss results.

A loop is used to fill an array with the characters h (head) and t (tail). As described above, use a random number to determine if each coin toss is a head or a tail. This array initialization is accomplished in the constructor (HINT: you can keep a count of the number of heads and tails as attributes as you fill the array).

Create methods that return the number of heads and the number of tails in the coin toss array (these are accessors).

Create a method that returns the total number of coin tosses.

Create a method that computes the longest (consecutive) number of heads or tails in the coin tosses array. This method stores this information in two attributes: one for the length of the longest run, and one for the outcome of that

longest run (head or tail). Initialize the length to 0.

To compute the longest run, keep two MORE values (not attributes, but local variables: one for the length of the current run of identical outcomes, and one for the outcome associated with the run (head or tail).

Initialize the outcome for the current run to neither a head nor a tail (perha[s anx) and the length of the run to 0.

In a loop, compare each toss in the array to the outcome of the current

run.

? If they match, the current run is now recognized as one longer (increment its length).

? If they dont match, then the current run has ended. Compare the length of this run to the longest run that has been previously seen (the longest run so far).

If this is current run is longer, record its length and outcome in the two attributes (above).

? Whether or not this is a new longest run, reset the current outcome to the one just examined (the mismatch) and reset the length of the current run to 1. Think about if any special processing needs to happen when the loop terminates.

Define accessor and mutator methods for the length and outcome of the longest run.

Define any additional methods as needed.

TestCoinToss.java

Prompt the user for the number of coin tosses.

You will repeat the run until the user enters any value less than 1.

TESTING

The results should be similar in nature to those shown in the sample run window below. Because we are using a random number generator, the results will change each time the application is executed. Use Scanner and a loop to get the number of coin tosses.

----jGRASP exec: java -ea -ea TestCoinToss

Enter integer number ( >= 2) coin tosses or 0 to Exit: 29

Number of Coin Tosses = 29 Fraction of Heads = 0.517 Fraction of Tails = 0.483 Longest run is 5 tails

Enter integer number ( >= 2) coin tosses or 0 to Exit: 725

Number of Coin Tosses = 725 Fraction of Heads = 0.520 Fraction of Tails = 0.480 Longest run is 9 heads

Enter integer number ( >= 2) coin tosses or 0 to Exit: 0

End of Program

----jGRASP: operation complete.

DETAILS

1. Submit the files to the appropriate MyCourses dropbox.

2. All calculations must be done in the CoinToss class.

3. Scanner should be used for input

4. Proper data types should be used.

PROBLEM 2 ArrayLists

Write a Java application that uses an ArrayList to store information about new cars. You will write three classes: NewCarListTester, NewCarList, and NewCar.

NewCar

The NewCar class is from HW6. Simply copy it from your HW6 solution and use it again here.

NewCarList

This is a class that has no constructors and a single attribute, an instantiated

ArrayList used to store information about NewCars. For example,

ArrayList carList = new ArrayList(); NewCarList will have the following four methods:

The add() method will accept a NewCar object and add it to the ArrayList.

The display() method will display, using toString(), the information about each object in the ArrayList .

The select(double maxPrice) method will do the same thing as display(), except that it will display only the cars with a final price less than or equal to the parameter maxPrice.

The drop(int index) method will delete the NewCar stored in the

ArrayList slot with the given index.

NewCarListTester

In NewCarListTester, the main method will instantiate five NewCars. Hard code the data for each car, using the NewCar constructor and other methods (e.g., calcFinalPrice). The actual data values are in the file HW09Data.xlsx.

For each car, create (i.e. instantiate) a NewCar object using a parameterized constructor for year, make and model (see HW06) and pass this object to the add method in NewCarList that uses that object as a parameter so it can be added to the ArrayList.

Included in NewCar is an attribute, finalPrice, with an appropriate accessor titled

getFinalPrice() accessor. NewCar also will provide the following methods:

calcFinalPrice(double stickerprice, double discount, double salesTaxRate)

calcZeroPctMontlyPayment(int numMonths) getCarName()

getCarAbbrev() toString

Create a NewCarListTester class that will:

1. Add five cars to the list (run calcFinalPrice for each). Again,the data input is hard-coded; i.e. there is no need to use Scanner input.

2. Display all the cars in the list.

3. Display all the cars under a certain final price (inclusive) (use the select method and be sure to test a boundary condition).

4. Add three more cars (run calcFinalPrice for each) and display the list.

5. Drop the second item in the list (What is the index?).

6. Display the cars again.

DETAILS

1. Submit your files to the MyCourses dropbox.

2. Carry out all calculations in the within the NewCar and NewCarList classes.

3. Use appropriate data types

4. Loop counts should not be hard coded (flexible number of cars in the list)

Output should resemble the following:

----jGRASP exec: java -ea -ea NewCarListTester

*** List of cars

You want to purchase a "2010 Kia Rio" Abbreviation: "10KR" Finalprice: 10800.0

You want to purchase a "2007 Kia Rio" Abbreviation: "07KR" Finalprice: 9720.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2010 Chevrolet Cobalt" Abbreviation: "10CC" Finalprice: 10800.0

*** List of cars under $9720

You want to purchase a "2007 Kia Rio" Abbreviation: "07KR" Finalprice: 9720.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

*** Add three more cars and list

You want to purchase a "2010 Kia Rio" Abbreviation: "10KR" Finalprice: 10800.0

You want to purchase a "2007 Kia Rio" Abbreviation: "07KR" Finalprice: 9720.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2010 Chevrolet Cobalt" Abbreviation: "10CC" Finalprice: 10800.0

You want to purchase a "2000 Honda Accord" Abbreviation: "00HA" Finalprice: 3240.0

You want to purchase a "2009 Suburu Forester" Abbreviation: "09SF"

Finalprice: 23760.0

You want to purchase a "2011 Checrolet Malibu" Abbreviation: "11CM" Finalprice: 23760.0

*** Delete second item in the list

You want to purchase a "2010 Kia Rio" Abbreviation: "10KR" Finalprice: 10800.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2004 Honda Civic" Abbreviation: "04HC" Finalprice: 6480.0

You want to purchase a "2010 Chevrolet Cobalt" Abbreviation: "10CC" Finalprice: 10800.0

You want to purchase a "2000 Honda Accord" Abbreviation: "00HA" Finalprice: 3240.0

You want to purchase a "2009 Suburu Forester" Abbreviation: "09SF" Finalprice: 23760.0

You want to purchase a "2011 Checrolet Malibu" Abbreviation: "11CM" Finalprice: 23760.0

----jGRASP: operation complete.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!