Question

In this assignment you will create two Java programs: The first program will be a class...

In this assignment you will create two Java programs:

The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods)

The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above.

You cannot use any of the home assignments as your submitted assignment.

Your programs must meet the following qualitative and quantitative criteria:

Your class must:

Contain at least 5 instance variables (0.5 marks)

Contain at least 2 methods other than the constructor (2 marks)

Provide a toString() method to print the data of an object in an appropriately formatted string (this does not count as one of the two methods above). (0.5 marks)

The main program must:

Read its data from System.In using a sentinel value to detect the end of the input. (1 mark)

Ensure that input errors will be handled correctly (0.5 marks)

Instantiate an array of objects of your class above, using the data input by the user to construct the objects (0.5 marks)

Test both methods of the class above (1 mark)

Print the content of the object array using the formatted printing method provided by the class (1 mark)

Have correct style (use of the standard template, use of comments, correct alignment of statements, good choice of names, appropriate use of case) (1 mark)

I really hope if I can get some actual examples for each requirement, Thank you very much :)

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

Hi Student,

Please find the code below for a Game Update. Lets say a cricket match is ongoing and you are displaying the average score and the run rate :

import java.util.ArrayList;
import java.util.Iterator;

//Implemented by Cricket data to communicate
//with observers
interface Subject
{
public void registerObserver(Observer o);
public void unregisterObserver(Observer o);
public void notifyObservers();
}

class CricketData implements Subject
{
private int runs;
private int wickets;
private float overs;
ArrayList<Observer> observerList;

public CricketData() {
observerList = new ArrayList<Observer>();
}

@Override
public void registerObserver(Observer o) {
observerList.add(o);
}

@Override
public void unregisterObserver(Observer o) {
observerList.remove(observerList.indexOf(o));
}

@Override
public void notifyObservers()
{
for (Iterator<Observer> it =
observerList.iterator(); it.hasNext();)
{
Observer o = it.next();
o.update(runs,wickets,overs);
}
}

// get latest runs from stadium
private int getLatestRuns()
{
// return 90 for simplicity
return 90;
}

// get latest wickets from stadium
private int getLatestWickets()
{
// return 2 for simplicity
return 2;
}

// get latest overs from stadium
private float getLatestOvers()
{
// return 90 for simplicity
return (float)10.2;
}

// This method is used update displays
// when data changes
public void dataChanged()
{
//get latest data
runs = getLatestRuns();
wickets = getLatestWickets();
overs = getLatestOvers();

notifyObservers();
}
}

//This interface is implemented by all those
//classes that are to be updated whenever there
//is an update from CricketData
interface Observer
{
public void update(int runs, int wickets,
float overs);
}

class AverageScoreDisplay implements Observer
{
private float runRate;
private int predictedScore;

public void update(int runs, int wickets,
float overs)
{
this.runRate =(float)runs/overs;
this.predictedScore = (int)(this.runRate * 50);
display();
}

public void display()
{
System.out.println("\nAverage Score Display: \n"
+ "Run Rate: " + runRate +
"\nPredictedScore: " +
predictedScore);
}
}

class CurrentScoreDisplay implements Observer
{
private int runs, wickets;
private float overs;

public void update(int runs, int wickets,
float overs)
{
this.runs = runs;
this.wickets = wickets;
this.overs = overs;
display();
}

public void display()
{
System.out.println("\nCurrent Score Display:\n"
+ "Runs: " + runs +
"\nWickets:" + wickets +
"\nOvers: " + overs );
}
}

//Driver Class
public class ObserverPattern
{
public static void main(String args[])
{
// create objects for testing
AverageScoreDisplay averageScoreDisplay =
new AverageScoreDisplay();
CurrentScoreDisplay currentScoreDisplay =
new CurrentScoreDisplay();

// pass the displays to Cricket data
CricketData cricketData = new CricketData();

// register display elements
cricketData.registerObserver(averageScoreDisplay);
cricketData.registerObserver(currentScoreDisplay);

// in real app you would have some logic to
// call this function when data changes
cricketData.dataChanged();

//remove an observer
cricketData.unregisterObserver(averageScoreDisplay);

// now only currentScoreDisplay gets the
// notification
cricketData.dataChanged();
}
}

Sample Output :

Average Score Display:
Run Rate: 8.823529
PredictedScore: 441

Current Score Display:
Runs: 90
Wickets:2
Overs: 10.2

Current Score Display:
Runs: 90
Wickets:2
Overs: 10.2

Happy Learning :)

Add a comment
Know the answer?
Add Answer to:
In this assignment you will create two Java programs: The first program will be a class...
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
  • In this assignment, you will write a Java program(s) to print the binary representation of a...

    In this assignment, you will write a Java program(s) to print the binary representation of a positive integer inserted from command line.  You must finish your assignment in 2 different ways: Using a recursive method Using an iterative method     Your main method must be: public static void main(String[] args) {      int input;         input = Integer.parseInt(args[0]);     print_recursion(input);     print_binary(input); }     You must implement a class and test your program by different input values, including 0. Comment your program properly Example of test...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • You will be creating a driver class and 5 class files for this assignment. The classes...

    You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • how would i write code in java/netbeans for this: You will be creating the driver class...

    how would i write code in java/netbeans for this: You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString(). The driver class must complete the following: Instantiate a TV object and set the initial channel to 97. Instantiate a...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Java program GCompound Practice Exercise CS141 Assignment Write a class that represents a digital snowman. Your...

    Java program GCompound Practice Exercise CS141 Assignment Write a class that represents a digital snowman. Your class should follow these guidelines: 1. Store the following private class variables bodyColor of type Color b. int x, int y for the upper left corner Graphics g. a. C. 2. Create two different constructor methods a. A (int x, int y, Graphics myG) parameter constructor that makes the snowman a light gray color by default and makes x and y the upper left...

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