Question

*URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone...

*URGENT JAVA PROGRAMMING LAB*

so I need to write some classes for my lab if someone could give me an outline or just how I should so it that would be awesom

here is the questions please help ASAP

A. A histogram is used to plot tabulated frequencies. Create a Histogram class that can be used to maintain and plot the frequencies of numbers that fall within a specified range. The Histogram class contain will an array of counters with equal intervals within the specified range (For example, see Test 1 and Test 2). All counters are first initialized to zero. When the number added to the Histogram falls within the range of a particular counter that counter is incremented by one. The Histogram class should contain the following methods:

• A constructor that accepts the number of counters and the maximum and minimum limits of the numbers (.i.e., the range of numbers).

• A second constructor that accepts the maximum and minimum limits of the numbers and initializes the Histogram with 10 counters for the specified range.

• An add(double x) method that increments the corresponding counter.

• A reset() method that sets all counter frequencies to zero. • A plotFrequency method that plots the counter frequencies. (See below)

• A plotCumulative method that plots the cumulative frequencies. (See below) Sample frequency and cumulative frequency plots with 5 counters:

Frequency Plot: Counter 1 : ******************************** (987)

Counter 2 : *****************************************(1014)

Counter 3: ***************************(950)

Counter 4: *********************************************(1065)

Counter 5: **************************************(984)

Cumulative Frequency Plot: Counter 1 : ****** (987)

Counter 2 : ***************(2001)

Counter 3: ***************************(2951)

Counter 4: ***************************************(4016)

Counter 5: **************************************************(5000)

Please note that the above plots are no to scale. You program should be able to determine the appropriate scale from the counter frequencies and scale the plot to fit within a printable page. Use the maximum frequency and total counter frequencies to scale the frequency and cumulative frequency plots.

Test cases: Write a RandomNumberTester class that uses the Histogram class to test the Math.random() method provided in the java SDK.

Test 1: Create 10 counters with the following limits:

Bin Number Limit

Counter 0 0 <= x < 0.1

Counter 1 0.1 <= x < 0.2

Counter 2 0.2 <=x < 0.3

Counter 3 0.3 <=x < 0.4

Counter 4 0.4 <= x < 0.5

Counter 5 0.5 <= x < 0.6

Counter 6 0.6 <= x < 0.7

Counter 7 0.7 <= x < 0.8

Counter 8 0.8 <= x < 0.9

Counter 9 0.9 <= x < 1.0

Generate a random number between 0 and 1. Increment the corresponding counter by 1. For example if the random number is 0.57, increment counter 5 by 1. Repeat this 10,000 times. Print the counter frequencies and the cumulative frequencies.

Test 2: Create 10 counters with the following limits:

Bin Number Limit Counter 0 0 <= x < 1

Counter 1 1 <= x < 2

Counter 2 2 <= x < 3

Counter 3 3 <= x < 4

Counter 4 4 <= x < 5

Counter 5 5 <= x < 6

Counter 6 6 <= x < 7

Counter 7 7 <= x < 8

Counter 8 8 <= x < 9

Counter 9 9 <= x < 10

Generate 10 random numbers between 0 and 1 and add them up. Increment the corresponding bin by 1. Repeat this 1,000 times. Print the counter frequencies and the cumulative frequencies. Any surprises/comments?

B. It is said that if a deck of cards is given perfect shuffles enough times, it will return to its original order. A perfect shuffle is done by splitting the deck exactly in half and interleaving the cards from the two halves; that is, the first card is from the first half, the second from the second half, the third from the first half, and so on. For example, Initial State:

AH 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH

AC 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC

AD 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD

AS 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS

After first shuffle: AH AD 2H 2D 3H 3D 4H 4D 5H 5D 6H 6D 7H

7D 8H 8D 9H 9D 10H 10D JH JD QH QD KH KD

AC AS 2C 2S 3C 3S 4C 4S 5C 5S 6C 6S 7C

7S 8C 8S 9C 9S 10C 10S JC JS QC QS KC KS

Create a Deck class with the following methods

• Deck() constructor that creates an unshuffled deck.

• A shuffle() method that does a perfect shuffle.

• A toString() method that print the deck as shown above.

• An equals(Deck aDeck) method that compares itself with the given deck and returns true if all the cards in both decks are in the same order and false otherwise

Write a DeckTester and test the Deck class by doing perfect shuffles on a deck. Start with two new decks in the main tester program and shuffle one deck. After each shuffle compare the shuffled deck with the unshuffled deck and see if they are in the same order. Shuffle up to 20 perfect shuffles or until the shuffled deck returns to its original order. The program should display the current order after each shuffle.

Thank you so much!

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

import java.util.Random;

class Counter {

   public double min;

   public double max;

      public double freq;

      public Counter(double min, double max){

      this.freq = 0.0;

      this.min = min;

      this.max = max;

      }

public double getMin() {

       return min;

   }

   public void setMin(double min) {

       this.min = min;

   }

   public double getMax() {

       return max;

   }

   public void setMax(double max) {

       this.max = max;

   }

public void increment() {

   this.freq += 1.0;

}

public void addVal(double x){

this.freq += x;

}

public double getFrequency(){

return this.freq;

}

public void resetFrequency(){

   this.freq = 0.0;

}

}

class Histogram {

public Counter[] counters;

public void setCounters(Counter[] counters) {

   this.counters = counters;

}

public static final int pageSize = 10000; //assuming page size is fixed to 10000.

public Histogram(int size, double min[], double max[]){

counters = new Counter[size];

for(int i = 0;i<size;i++){

counters[i] = new Counter(min[i], max[i]);

}

}

public Histogram(double min[], double max[]){

counters = new Counter[10];

for(int i = 0;i<10;i++){

       counters[i] = new Counter(min[i], max[i]);

}

}

public Counter[] getCounters() {

       return counters;

}

public void add(Counter c, double x){

c.addVal(x);

}

public void reset(Counter c){

c.resetFrequency();

}

public double getMaxFreq(){

double max = Double.MIN_VALUE;

for(int i = 0;i<counters.length;i++){

if(counters[i].freq > max)

max = counters[i].freq;

}

return max;

}

public double getMinFreq(){

double min = Double.MAX_VALUE;

for(int i = 0;i<counters.length;i++){

if(counters[i].freq < min)

min = counters[i].freq;

}

return min;

}

public double getTotalFreq(){

double total = 0.0;

for(int i = 0;i<counters.length;i++){

total += counters[i].freq;

}

return total;

}

public String getBlockString(int size){

StringBuilder sb = new StringBuilder();

for(int i =1;i<=size;i++)

sb.append('*');

return sb.toString();

}

public void plotFrequency() {

//first find the bock size to utilize the whole page

int blockSize = (int) (pageSize/getMaxFreq());

for(int i =0;i<counters.length;i++){

int counterSize = (int)(blockSize * counters[i].getFrequency());

System.out.println("Counter " + (i+1) + ": " + getBlockString(counterSize));

}

}

public void plotCumulative() {

//first find the bock size to utilize the whole page

int blockSize = (int) (pageSize/getTotalFreq());

double cumFreq = 0.0;

for(int i =0;i<counters.length;i++){

cumFreq += counters[i].getFrequency();

int counterSize = (int)(blockSize * cumFreq);

System.out.println("Counter " + (i+1) + ": " + getBlockString(counterSize));

}

}

}

public class RandomNumberTester {

public static void main(String[] args){

//take test 1 data

double min[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};

double max[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0};

Histogram test1 = new Histogram(min, max);

Random rand = new Random();

for(int i=1;i<=10000;i++) {

       double n = rand.nextDouble();

       Counter[] c = test1.getCounters();

       for(int j = 0;j<c.length; j++) {

           if(c[j].min <= n && n <= c[j].max) {

               c[j].increment();

           continue;

           }

       }

}

test1.plotFrequency();

test1.plotCumulative();

}

}

PROBLEM 2

======================

import java.util.List;

import java.util.ArrayList;

import java.util.Arrays;

class Deck {

   private ArrayList<String> state;

   public Deck(List<String> state) {

       this.state = new ArrayList<String>(state);

   }

   public void shuffle() {

       int len = state.size();

       ArrayList<String> deck1 = new ArrayList<String>(state.subList(0, len/4));

       ArrayList<String> deck2 = new ArrayList<String>(state.subList(len/4, 2*(len/4)));

       ArrayList<String> deck3 = new ArrayList<String>(state.subList( 2*(len/4), 3*(len/4)));

       ArrayList<String> deck4 = new ArrayList<String>(state.subList( 3*(len/4), len));

       state = new ArrayList<String>();

  

for(int i =0;i<len/4;i++) {

           state.add(deck1.get(i));

           state.add(deck3.get(i));

           state.add(deck2.get(i));

           state.add(deck4.get(i));

       }

   }

   public String toString() {

       StringBuilder sb =new StringBuilder();

       for(int i =0;i<state.size();i++) {

           sb.append(state.get(i));

           sb.append(" ");

       }

       return sb.toString();

   }

  

   public void setState(ArrayList<String> state) {

       this.state = state;

   }

  

   public ArrayList<String> getState() {

       return state;

   }

  

   public boolean equals(Deck d) {

       return this.state.toString().equals(d.getState().toString());

   }

}

public class Test {

   public static void main(String[] args) {

       List<String> state = Arrays.asList(

               "AH", "2H", "3H", "4H",

               "AC", "2C", "3C", "4C",

               "AD", "2D", "3D", "4D",

               "AS", "2S", "3S", "4S");

       Deck deck1 =new Deck(state);

       Deck deck2 = new Deck(state);

       for(int i =0;i<20;i++) {

           deck1.shuffle();

           System.out.println("Shuffled Deck : " + deck1.toString());

           if(deck1.equals(deck2)) {

               System.out.println("Two decks are same. Returning from main method");

               return;

           }

       }

      

   }

}

Add a comment
Know the answer?
Add Answer to:
*URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone...
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
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