Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an Object oriented application that flips virtual coins and keeps track o

ID: 3606302 • Letter: C

Question

Create an Object oriented application that flips virtual coins and keeps track of the results in a JList. Keep count of how many doubles and triples are flipped (i.e., two or three of the same side in a row) and display the running counts (of doubles and triples) in 2 textfields or labels.

Each time a button is pressed, flip a virtual coin(see below) and add the result to the list and update the 2 results.

Use a class to describe doubloons, for example.,:

class coin {

int side = -1;

String[] side$ = {"Heads", "Tails"};

public void flip() {

side = r.nextInt(2);

System.out.println(side$[side]);

}

public String toString() {

return side$[side];

}

}

This is just an example!

You can create a new coin objects using

coin d = new coin();

And flip the coin using:

d.flip();

Explanation / Answer

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Coin d = new Coin();

Scanner sc = new Scanner(System.in);

while(!sc.next().equals("n"))

d.flip();

}

}

import java.util.Random;

import javax.swing.DefaultListModel;

import javax.swing.JList;

public class Coin {

int side = -1;

String[] side$ = {"Heads", "Tails"};

String t1=null, t2=null, t3=null;

Random r = new Random();

int triples=0;

int doubles=0;

DefaultListModel<String> l1 = new DefaultListModel<>();   

JList<String> myList = new JList<String>();

public void flip() {

side = r.nextInt(2);

System.out.println(side$[side]);

l1.addElement(side$[side]);

myList = new JList<>(l1);

t1 = t2;

t2 = t3;

t3 = side$[side];

  

if(t1!=null && t2!=null && t1.equals(t2) && t2.equals(t3))

triples++;

if(t2!=null && t2.equals(t3))

doubles++;

  

System.out.println("Double count = "+doubles);

System.out.println("Triples count = "+triples);

}

public String toString() {

return side$[side];

}

}

OUTPUT:

w
Tails
Double count = 0
Triples count = 0
e
Heads
Double count = 0
Triples count = 0
q
Tails
Double count = 0
Triples count = 0
q
Heads
Double count = 0
Triples count = 0
q
Tails
Double count = 0
Triples count = 0
q
Tails
Double count = 1
Triples count = 0
q
Tails
Double count = 2
Triples count = 1
q
Tails
Double count = 3
Triples count = 2
q
Heads
Double count = 3
Triples count = 2
e
Heads
Double count = 4
Triples count = 2
n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote