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

In this assignment you will create two Java programs: 1. The first program will

ID: 3902064 • Letter: I

Question

In this assignment you will create two Java programs: 1. The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) 2. 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: A. Your class must: 1. Contain at least 5 instance variables (0.5 marks) 2. Contain at least 2 methods other than the constructor (2 marks) 3. 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) B. The main program must: 1. Read its data from System.In using a sentinel value to detect the end of the input. (1 mark) 2. Ensure that input errors will be handled correctly (0.5 marks) 3. Instantiate an array of objects of your class above, using the data input by the user to construct the objects (0.5 marks) 4. Test both methods of the class above (1 mark) 5. Print the content of the object array using the formatted printing method provided by the class (1 mark) 6. 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) Can someone please help me by giving an example? Or even an example of each requirement with an explanation. Thanks!

Explanation / Answer


As per your requirement i have written code for Game update which fulfill all your requirements please follow it step by step.


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;

//Here we are going to implement by Cricket data to communicate

//That too by using observers

interface Subject
{
public void registerObserver(Observer observerObject);
public void unregisterObserver(Observer observerObject);
public void notifyObservers();
}

class Cricketdata implements Subject
{
private int runsCount;
private int wicketsCount;
private float oversCount;

ArrayList<Observer> observerlistObject;

public Cricketdata() {
observerlistObject = new ArrayList<Observer>();
}

@Override
public void registerObserver(Observer observerObject) {
observerlistObject.add(observerObject);
}

@Override
public void unregisterObserver(Observer observerObject) {
observerlistObject.remove(observerlistObject.indexOf(observerObject));
}

@Override
public void notifyObservers()
{

for (Iterator<Observer> iteratorObject =
observerlistObject.iterator(); iteratorObject.hasNext();)
{
Observer observerObject = iteratorObject.next();
observerObject.updateMethod(runsCount,wicketsCount,oversCount);
}
}

// Here we will get latest runs Count from stadium
private int getLatestrunsCall()
{

// Here simply we will return 90
return 90;
}

//Here we are going to get latest wickets Count from stadium
private int getLatestwicketscall()
{
// Here simply we will return 2

return 2;
}

//Here we are going to get latest overs Count from stadium
private float getlatestoversCall()
{
// Here simply we will return 90

return (float)10.2;
}

// Here we will write this method to updateMethod displays


public void datachangedMethod()
{
//Here we will get the latest data

runsCount = getLatestrunsCall();

wicketsCount = getLatestwicketscall();

oversCount = getlatestoversCall();

notifyObservers();
}
}


interface Observer
{

public void updateMethod(int runsCount, int wicketsCount,
float oversCount);


}

class AveragescoredisplayClass implements Observer
{
private float runrateValue;
private int predictedscoreValue;

public void updateMethod(int runsCount, int wicketsCount,
float oversCount)
{

this.runrateValue =(float)runsCount/oversCount;

this.predictedscoreValue = (int)(this.runrateValue * 50);
display();
}

public void display()
{
System.out.println(" Average Score Display: "
+ "Run Rate: " + runrateValue +
" PredictedScore: " +
predictedscoreValue);
}
}

class CurrentscoredisplayClass implements Observer
{

private int runsCount, wicketsCount;

private float oversCount;

public void updateMethod(int runsCount, int wicketsCount,
float oversCount)
{
this.runsCount = runsCount;

this.wicketsCount = wicketsCount;
this.oversCount = oversCount;

display();
}

public void display()
{
System.out.println(" Current Score Display: "
+ "Runs: " + runsCount +
" Wickets:" + wicketsCount +
" Overs: " + oversCount );
}


}


public class ObserverPattern
{
public static void main(String args[])
{
// Here we will create objects for testing
AverageScoreDisplay averagescoredisplayObject =
new AverageScoreDisplay();

CurrentscoredisplayClass currentscoredisplayObject =
new CurrentscoredisplayClass();

//Actually here we will pass the displays to Cricket data

Cricketdata cricketDataObject = new Cricketdata();

// And here we will register display elements
cricketDataObject.registerObserver(averagescoredisplayObject);
cricketDataObject.registerObserver(currentscoredisplayObject);


cricketDataObject.datachangedMethod();

//Here we will remove observer

cricketDataObject.unregisterObserver(averagescoredisplayObject);


cricketDataObject.datachangedMethod();

}

}

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