Question: Include one or more Exception classes in the project so that you can throw an exception if an object is null or there is an

Include one or more Exception classes in the project so that you can throw an exception if an object is null or there is an error.

For this problem, you will have to write TWO classes. Use the Player class from PP 1 and write another class that is meaningful to you. In the driver, you will create objects of these classes, and manipulate them using two stacks, with one for each class. You have to make sure you test all methods in the Stack class meaningfully. You also have to make sure you test all methods in the two classes you have written while manipulating the objects using the Stack data structure, meaningfully. In the Driver, you must test all methods in a self-documentary way. When the output is displayed, it should be self-explanatory. The reader should not have to look at your code to understand the output. PP1 Player Class

package main;

//Player.java import java.util.Random; public class Player { // instance variables private String name; private int score; private String rank;

public Player() { this.score = 0; } public Player(String name) { this(); this.name = name; }

// Accessors and Mutator public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getScore() { return score; }

// create random score public void play() { Random random = new Random(); for (int i=0; i<10; i++){ this.score += random.nextInt(10); } // decide the rand based on score this.rank = decideRank(); }

// decide the rand based on score public String decideRank(){ if (score >=50) return "Level 1"; else if(score >= 30) return "Level 2"; else if( score >= 20) return "Level 3"; else return "Level 4"; }

public String getRank() { return rank; }

@Override public String toString() { return "Player: " +name + " " + "Score:" + score + " " + "Rank: " + rank + " "; } }

Make sure to test all methods in the two classes while manipulating the objects using the Stack data structure, meaningfully.

In the driver, test the play method, the decideRank method, etc. Test the methods of an object while manipulating the stack.

Make sure it is in Javadoc style documentation.

Test Player Class here

package player;

//custome excetion class to throw exception

public class CustomException extends Exception{

CustomException(String e){

super(e);

}

}

package player;

//player class and its properties

public class Player {

String name;

int age;

Game plays;

int rank;

//Constructor to initialize field

public Player(String name, int age, Game plays, int rank) {

super();

this.name = name;

this.age = age;

this.plays = plays;

this.rank = rank;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

//throw exception when age is age<18 or age>50

public void setAge(int age) {

try{

if(age<18 || age>50)

throw new CustomException("Age can not be greater then 50 or less then 18");

this.age = age;

}

catch(Exception e){

System.out.println(e);

}

}

public Game getPlays() {

return plays;

}

public void setPlays(Game plays) {

this.plays = plays;

}

public int getRank() {

return rank;

}

public void setRank(int rank) {

this.rank = rank;

}

}

//game class and its methods and data member

class Game{

String name;

String rules;

String popularity;

String origin;

String playedIn;

public Game(String name, String rules, String popularity, String origin, String playedIn) {

super();

this.name = name;

this.rules = rules;

this.popularity = popularity;

this.origin = origin;

this.playedIn = playedIn;

}

//getters setters

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getRules() {

return rules;

}

public void setRules(String rules) {

this.rules = rules;

}

public String getPopularity() {

return popularity;

}

public void setPopularity(String popularity) {

this.popularity = popularity;

}

public String getOrigin() {

return origin;

}

public void setOrigin(String origin) {

this.origin = origin;

}

public String getPlayedIn() {

return playedIn;

}

public void setPlayedIn(String playedIn) {

this.playedIn = playedIn;

}

}

package player;

import java.util.Stack;

//driver function to print output using stack class

public class Driver {

public static void main(String[] args) {

Stack game=new Stack<>();

Stack players=new Stack<>();

Game g=new Game("hokey", "set my capton", "very", "Mexico", "whole world");

game.add(g);

Game g1=new Game("Cricket", "set my Empire", "Not Much", "England", "Mexico");

game.add(g1);

Player p=new Player("Mr Spock", 28, g, 4);

players.add(p);

p=new Player("Sachin", 30, g1, 10);

players.add(p);

p=new Player("Cristine", 40, g1, 10);

p.setAge(60);

players.add(p);

System.out.println("-------------------Game info----------------------------");

while(!game.isEmpty()){

Game gm=game.pop();

System.out.println("Game currently being played is:"+gm.getName());

System.out.println("This game is "+gm.getPopularity()+" populer");

System.out.println("This Game played in "+gm.getPlayedIn());

System.out.println("Its originated from "+gm.getOrigin());

System.out.println("-----------------------------------------------");

}

System.out.println("-------------------Player Info----------------------------");

while(!players.isEmpty()){

Player pl=players.pop();

System.out.println("Player currently playing is:"+pl.getName());

System.out.println("He plays "+pl.getPlays().getName());

System.out.println("His overall rank is "+pl.getRank());

System.out.println("His age is "+pl.getAge());

System.out.println("-----------------------------------------------");

}

}

}

Purpose:

Incorporate classes with exceptions to make the curriculum robust and elegant.

Define a Stack data structure interface so that the interface can be enforced by classes

By defining the Stack data structure class with an array, implement the Stack interface

By defining your own classes (two of them) to describe two different kinds of objects.

Create Stack objects to organize other objects so as to easily manipulate them ( accessing/modifying/updating/deleting)

Fully test a defined class by invoking each method of a class.

Employ the packaging structure to organize your classes

Employ the javadoc tool to write and generate documentation/API

Most the code is already provided just need to make the revisions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!