In this assignment you will create two Java programs: The first program will be
ID: 3735199 • Letter: I
Question
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 :)
Explanation / Answer
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(" Average Score Display: "
+ "Run Rate: " + runRate +
" PredictedScore: " +
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(" Current Score Display: "
+ "Runs: " + runs +
" Wickets:" + wickets +
" Overs: " + 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 :)