Question

In: Computer Science

Complete the Monster Attack project by doing the following: In place of the driver used in...

Complete the Monster Attack project by doing the following:

In place of the driver used in homework 2, add a menu with a loop and switch. The user must be able to add attacks, get a report on existing attacks, sve a file, retrieve a file, and clear the list as many times as she wants in any order she wants.

Replace the array of MonsterAttacks with an ArrayList of MonsterAttacks. This will require changes in several methods. You will need to be able to handle any number of MonsterAttacks the user chooses to input.

Add a method to AttackMonitor that saves the list of attacks to a comma separated values file. Iterate through the list, and for each attack, get each field using the getters from MonsterAttack. Write each value to the file, following each one except the last with a comma. Save the date as a single String in the format MM/DD/YYYY. After you have written out all the data for one attack, write out a newline. Add an item to the main menu that calls this method.

Add a method that clears the list of monster attacks, then uses a Scanner to read data from a .csv file, uses it to instantiate MonsterAttack objects, and adds the attacks to the list. This method must be able to read the files you write out in the method described above. You will need to use String's split() method here. Add an item to the main menu that calls this method. Make sure you can input attack data, save to a file, quit the program, start the program again, read your output file, and show the data from the file.

this is in java code

AttackMonster.java
import java.util.Scanner;
import java.util.Date;
public class AttackMonster {
public MonsterAttack[] attacks;
public void reportAttacks() {
this.attacks = new MonsterAttack[5];
int i;
Scanner scan;
for(i=0; i<5; i++) {
scan = new Scanner(System.in);
System.out.print("Enter the name of the monster: ");
String m = scan.nextLine();
System.out.print("Enter location of attack: ");
String l = scan.nextLine();
System.out.print("Enter amount of damage: ");
double dam = scan.nextDouble();
System.out.print("Enter date of attack: ");
int d = scan.nextInt();
System.out.print("Enter month of attack: ");
int mon = scan.nextInt();
System.out.print("Enter year of attack: ");
int y = scan.nextInt();
@SuppressWarnings("deprecation")
Date date = new Date(d, mon, y);
MonsterAttack ma = new MonsterAttack(m,l, dam, date);
attacks[i] = ma;
} //End of for
}
  
public void showAttacks() {
int i;
for(i=0; i<5; i++) {
System.out.println(this.attacks[i].toString());
}
}
  
public void calculateDamages() {
double mean=0.0;
int i;
for(i=0; i mean = this.attacks[i].damagesInMillionUSD;
}
mean = mean/this.attacks.length;
System.out.println("Mean damages of attacks in millions USD: " + mean);
}
  
public void showMonsters() {
for(MonsterAttack ma: this.attacks) {
System.out.println(ma.getMonsterName());
}
}
  
public void getEarliestAttack() {
MonsterAttack m = this.attacks[0];
for(MonsterAttack ma: this.attacks) {
if(m.getDate().after(ma.getDate())) {
m = ma;
}
}
System.out.println("Earliest attack: \n" + m.toString());
}
}

MonsterAttack.java

import java.util.Date;

public class MonsterAttack {
String monsterName;
String attackLocation;
double damagesInMillionUSD;
Date date;
public MonsterAttack(String monsterName, String attackLocation, double damagesInMillionUSD, Date date) {
super();
this.monsterName = monsterName;
this.attackLocation = attackLocation;
this.damagesInMillionUSD = damagesInMillionUSD;
this.date = date;
}
/**
* return the monsterName
*/
public String getMonsterName() {
return monsterName;
}
/**
* param monsterName the monsterName to set
*/
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
/**
* return the attackLocation
*/
public String getAttackLocation() {
return attackLocation;
}
/**
* param attackLocation the attackLocation to set
*/
public void setAttackLocation(String attackLocation) {
this.attackLocation = attackLocation;
}
/**
* return the damagesInMillionUSD
*/
public double getDamagesInMillionUSD() {
return damagesInMillionUSD;
}
/**
* \param damagesInMillionUSD the damagesInMillionUSD to set
*/
public void setDamagesInMillionUSD(double damagesInMillionUSD) {
this.damagesInMillionUSD = damagesInMillionUSD;
}
/**
* return the date
*/
public Date getDate() {
return date;
}
/**
* param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
public String toString() {
String str = "Monster name: " + this.monsterName+
" Attack Location: " + this.attackLocation+
" Damage in millions USD : " + this.damagesInMillionUSD+
" Date of attack: " + this.date;
return str;
}
}

MonsterDriver.java

public class MonsterDriver {
   public static void main(String[] args) {
       AttackMonster am = new AttackMonster();
       am.reportAttacks();
am.showAttacks();
am.showMonsters();
am.calculateDamages();
am.getEarliestAttack();
}
   }

This was part 1 to code the first part: Please help me asap

Part A

Create a MonsterAttack class. MonsterAttack has the following private data variables. Think about the correct data type to use for each variable:

monsterName (eg Godzilla)

attackLocation (eg Tokyo)

damagesInMillionUSD (eg 123.45)

date (eg October 27, 1954, OK to use the Java Date class)

Create whatever constructors, getters, and setters you need. Be sure to write a reasonable toString().

Part B

Create an AttackMonitor class. AttackMonitor contains an array of Monster attacks and has methods to generate reports on the attacks. It should include at least these methods:

reportAttacks() creates an array of five attacks, takes user input using a Scanner from System.in, creates the attacks (instances of MonsterAttack), and adds the attacks to the array.

showAttacks() iterates through the array and prints out the result of running the toString() of each attack.

showDamages() calculates and prints the total amount of damages for all attacks and the mean damages

showMonsters() shows the names of all monsters involved in the attacks and the number of attacks for each monster (ie: "Godzilla, 3 attack(s); Bigfoot, 1 attack(s); Yeti: 1 attack(s)). Since we may discover new monsters after compile time, you must find the names using a getter in MonsterAttack. Don't hard- code the monster names.

findEarliestAttack() prints out the to String() of the earliest MonsterAttack in the array.

A menu method that offers the user the opportunity to run any of the methods listed above or to quit. The menu must be contained in a loop with a swtich statement, as shown in several lecture examples.. The user can do any of the above tasks in any order, as many times as she wants to.

Part C

Write a main() method for AttackMonitor. Think about what it needs to do to show that the other classes work correctly. The driver should only call methods from AttackMonitor, not methods from MonsterAttack.


what else do you need ? i Have provided with all the info just make the adjustments from what its askinf at first to the code I posted

Solutions

Expert Solution

CODE:

MonsterDriver.java

package monsterdriver;

import java.io.File;
import java.util.Scanner;

public class MonsterDriver {
public static void main(String[] args) {
boolean fileread=false;
File file = new File("Attacks.csv");
AttackMoniter am = new AttackMoniter();
boolean loop=true;
while(loop)
{
System.out.println("Enter Choice 1)Report Attacks 2)Show Attacks 3)Show Monsters 4)Calculate Damages 5)Get Earliest Attack 6)Save To File 7)Read From File 8)Clear List\n");
Scanner in=new Scanner(System.in);
int choice=in.nextInt();

switch(choice)
{
case 1:{
am.reportAttacks();break;
}
case 2:{
am.showAttacks();break;
}
case 3:{
am.showMonsters();break;
}
case 4:{
am.calculateDamages();break;
}
case 5:{
am.getEarliestAttack();break;
}
case 6:{
am.saveToFile(file);break;
}
case 7:{
if(!fileread)
{
am.readFromFile(file);
fileread=true;//To avoid duplicate data
break;
}
}
case 8:{
am.clearList();break;
}
default : loop=false;
}
}
}
}

AttackMoniter.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monsterdriver;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AttackMoniter {
public List<MonsterAttack> attacks =new ArrayList<MonsterAttack>();
public void reportAttacks() {
Scanner scan;
scan = new Scanner(System.in);
System.out.print("Enter the name of the monster: ");
String m = scan.nextLine();
System.out.print("Enter location of attack: ");
String l = scan.nextLine();
System.out.print("Enter amount of damage: ");
double dam = scan.nextDouble();
System.out.print("Enter date of attack: ");
int d = scan.nextInt();
System.out.print("Enter month of attack: ");
int mon = scan.nextInt();
System.out.print("Enter year of attack: ");
int y = scan.nextInt();
Calendar calendar = Calendar.getInstance();
calendar.set(y,mon-1,d);
Date date =calendar.getTime();
MonsterAttack ma = new MonsterAttack(m,l, dam, date);
attacks.add(ma);

}
  
public void showAttacks() {
int i;
for(i=0; i<attacks.size(); i++) {
System.out.println(this.attacks.get(i).toString());
}
}
  
public void calculateDamages() {
double mean=0.0;
int i;
for(i=0; i<this.attacks.size();i++){
mean += this.attacks.get(i).getDamagesInMillionUSD();
}
System.out.println("Total damages of attacks in millions USD: " + mean);
mean = mean/this.attacks.size();
System.out.println("Mean damages of attacks in millions USD: " + mean);
}
  
public void showMonsters() {
Map<String, Integer> map=new HashMap<String, Integer>();
for(int i=0; i<this.attacks.size();i++){
if(!map.containsKey(attacks.get(i).getMonsterName()))
{
map.put(attacks.get(i).getMonsterName(), 1);
}
else
{
map.put(attacks.get(i).getMonsterName(), map.get(attacks.get(i).getMonsterName())+1);
}
}
for (String name: map.keySet()){
String key = name;
String value = map.get(name).toString();
System.out.println(key + " " + value+"attack(s)");
}
}
public void getEarliestAttack() {
MonsterAttack m = this.attacks.get(0);
for(MonsterAttack ma: this.attacks) {
if(m.getDate().after(ma.getDate())) {
m = ma;
}
}
System.out.println("Earliest attack: \n" + m.toString());
}
public void saveToFile(File file)
{
try{
FileWriter writer=new FileWriter(file);
String header="MonsterName,Location,DamagePrice,Date\n";
writer.write(header);
for(int i=0;i<attacks.size();i++)
{
writer.append(attacks.get(i).toStringCSV());
}
writer.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void readFromFile(File file)
{
try{
Scanner reader = new Scanner(file);
String header=reader.nextLine();
while (reader.hasNextLine()) {
String data = reader.nextLine();
String[] str=data.split(",");
Date date=new SimpleDateFormat("dd/MM/yyyy").parse(str[3]);
MonsterAttack ma = new MonsterAttack(str[0],str[1], Double.parseDouble(str[2]), date);
if(!attacks.contains(ma))
{
attacks.add(ma);
}
}
reader.close();
}
catch(Exception e)
{
  
}
}
public void clearList()
{
attacks.clear();
}
}

MonsterAttack.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monsterdriver;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MonsterAttack {
String monsterName;
String attackLocation;
double damagesInMillionUSD;
Date date;
public MonsterAttack(String monsterName, String attackLocation, double damagesInMillionUSD, Date date) {
super();
this.monsterName = monsterName;
this.attackLocation = attackLocation;
this.damagesInMillionUSD = damagesInMillionUSD;
this.date = date;
}

public String getMonsterName() {
return monsterName;
}

public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}

public String getAttackLocation() {
return attackLocation;
}

public void setAttackLocation(String attackLocation) {
this.attackLocation = attackLocation;
}

public double getDamagesInMillionUSD() {
return damagesInMillionUSD;
}

public void setDamagesInMillionUSD(double damagesInMillionUSD) {
this.damagesInMillionUSD = damagesInMillionUSD;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
public String toString() {
String str = "Monster name: " + this.monsterName+
" Attack Location: " + this.attackLocation+
" Damage in millions USD : " + this.damagesInMillionUSD+
" Date of attack: " + this.date;
return str;
}
public String toStringCSV() {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String str = this.monsterName +","+ this.attackLocation+"," + this.damagesInMillionUSD+"," +formatter.format(this.date)+"\n";
return str;
}

}

OUTPUT:

SECOND RUN


Related Solutions

An indy 500 driver is doing 105 m/s, with 100,000 meters and 10s left in a...
An indy 500 driver is doing 105 m/s, with 100,000 meters and 10s left in a race. Her car can do a maximum acceleration of 15 m/s^2. Can she finish the race? Does she need to accelerate the entire time? If not, what is the minimum amount of time she must accelerate in order to finish the race in 10s?
Complete the following steps: • TWO APPLIED FORCES – Place a pulley at the 20◦ mark...
Complete the following steps: • TWO APPLIED FORCES – Place a pulley at the 20◦ mark on the force table and place a total of 0.1 kg(including the mass holder) on the end of the string. Calculate the magnitude of the force produced by the mass using g = 9.8 m/s2.Assume three significant figures for this and all other calculations of force. Record the value of this force as F1. – Place a second pulley at the 90◦ mark on...
Complete the following nuclear equations: When doing these equations you are obeying the law of conservation...
Complete the following nuclear equations: When doing these equations you are obeying the law of conservation of mass and charge. What this means is that the sum of top numbers (masses) on the left side must equal the sum of the masses on the right. It also means the sum of the bottom numbers (charges) on the left equal the sum of the charges on the right. The charge (atomic number) of the isotope determines the element. Look on the...
Complete a driver diagram on improving patient satisfaction in the radiology department or on a current...
Complete a driver diagram on improving patient satisfaction in the radiology department or on a current issue you are dealing with at work. I work at Cvs Health Minute Clinic , a current issue is wait time Complete a flow chart on one of the processes in the issues you did your driver diagram on.
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
An attack rate is an alternative incidence rate that is used when: * A. describing the...
An attack rate is an alternative incidence rate that is used when: * A. describing the occurrence of food-borne illness or infectious diseases. B. the population at risk increases greatly over a short time period. * C. the disease rapidly follows the exposure during a fixed time period. D. all of the above.
A researcher doing interviews with immigrants in the U.S. records the place of birth for each...
A researcher doing interviews with immigrants in the U.S. records the place of birth for each of her ten respondents. Her data is shown in the following table. Person Place of Birth Person Place of Birth 1 North America 6 Africa 2 Central America 7 Africa 3 Central America 8 Africa 4 South America 9 Europe 5 Asia 10 Europe Complete the following frequency table. (10 points) Place of Birth (X) f % Sketch a bar graph or histogram for...
* Should companies be doing more to make the world a better place? Yes or No?...
* Should companies be doing more to make the world a better place? Yes or No? Defend your answer in completeness.
* Should companies be doing more to make the world a better place? Yes or No?...
* Should companies be doing more to make the world a better place? Yes or No? Defend your answer in completeness.
Which of the following statements is true? Project management is becoming a standard way of doing...
Which of the following statements is true? Project management is becoming a standard way of doing business Project management is replacing middle management as a way to get things done Project management is used primarily with large projects Both A and B are true A, B and C are all true
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT