Find and correct all the syntax, logic, and runtime errors. Download the attache
ID: 3775959 • Letter: F
Question
Find and correct all the syntax, logic, and runtime errors.
Download the attached program, and run it. There are at least 3 problems/bugs in the program. Find and correct each of the problems. Be sure to verify that you have found all of the bugs by showing your program to one of the LAs or the TA in the lab.
Additionally, make sure you put a comment inside of each method. This will be part of your grade in this lab. Good luck!
package Errors;
/**
*This is the domain class for a lab meant to test error-checking.
*
* Now where did I leave my comments...? Oh well, guess you'll have to figure it out.
* @author Kevin Criado
*/
public class ErrorDomain {
private int numZombies;
private int numBullets;
private boolean alive;
public ErrorDomain(int aZombies, int someBullets){
numZombies = aZombies;
numBullets = someBullets;
alive = true;
}
//What variable type are we trying to return?
//HINT: It's not String
public String getNumZombies()
{
return numZombies;
}
//What variable type are we trying to return?
public boolean getNumBullets()
{
return numBullets;
}
public boolean getAlive()
{
return alive;
}
public void setNumZombies(double zombies)
{
//What exactly does possible lossy conversion mean?
//Think about it. If we turn 3.26564548112 into an Integer we get: 3
//3 isn't the same as 3.26564548112
numZombies = zombies;
}
public void setNumBullets(double bullets)
{
//See setNumZombies(), this is a similar problem.
numBullets = bullets;
}
public void setAlive()
{
//This is an easy one. Fix it!
alive = !alive
}
public String toString()
{
String str = "There are currently " + numZombies + " zombies, and you have " + numBullets + " bullets left.";
if (!alive)
{
str += " Unfortunately, you are dead.";
}
if (numZombies == 0)
{
str += " Congratulations on surviving the zombie apocalypse!";
}
return str;
}
}
package Errors;
/**
* This is the driver for a class that is meant for practice on error checking.
*
* I think I misplaced all my comments. Darn. Good luck and have fun!
* @author Kevin Criado, and Matt messed it up.
*/
public class ErrorTester {
public static ErrorDomain zombieApoc;
public static Random helpMe = new Random();
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//We are trying to call non-static methods from a static method, how do we fix this?
beginApocalypse();
survive();
}
public void beginApocalypse()
{
System.out.println("SUDDENLY, ZOMBIES!");
int zombies = helpMe.nextInt(30)+5;
int bullets = helpMe.nextInt(10)+10;
zombieApoc = new ErrorDomain(zombies, bullets);
}
public void survive()
{
Scanner kb = new Scanner(System.in);
while (zombieApoc.getAlive() && zombieApoc.getNumZombies() != 0)
{
System.out.println(zombieApoc);
System.out.println("What do you do?");
System.out.println("1. Shoot a zombie.");
System.out.println("2. Look for more bullets.");
System.out.println("3. Kill all remaining zombies.");
System.out.println("4. Give up.");
int choice = kb.nextInt();
//For some reason every case below the number you selected executes.
//I think our switch statement is broken, or maybe it's just missing something.
switch (choice)
{
//We are expecting a 1 here but this doesn't work. How can I fix this?
case "1":
if (zombieApoc.getNumBullets() > 0)
{
System.out.println("You shot a zombie successfully!");
zombieApoc.setNumZombies(zombieApoc.getNumZombies() - 1);
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 1);
}
else
{
System.out.println("You are out of bullets! Why didn't you think this through?!");
zombieApoc.setAlive();
}
//I think we are missing something here.
case "2":
int successCheck = helpMe.nextInt(40);
if (successCheck > zombieApoc.getNumZombies())
{
int moreBullets = helpMe.nextInt(3)+1;
zombieApoc.setNumBullets(zombieApoc.getNumBullets() + moreBullets);
System.out.println("You found some bullets! You now have " + zombieApoc.getNumBullets() + " bullets.");
}
else if (zombieApoc.getNumBullets() > 1)
{
System.out.println("Oh no! More zombies! You killed one but wasted a bullet in panic!");
zombieApoc.setNumZombies(zombieApoc.getNumZombies() - 1);
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 2);
}
else
{
System.out.println("Oh no! Zombies! And you're out of bullets! Aaaaaaaa!");
zombieApoc.setAlive();
}
//And here.
case "3":
if (zombieApoc.getNumBullets() >= zombieApoc.getNumZombies())
{
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - zombieApoc.getNumZombies());
zombieApoc.setNumZombies(0);
System.out.println("You successfully take out all remaining zombies.");
}
else
{
System.out.println("You need one bullet for every zombie still left to use this command.");
}
//And here.
case "4":
System.out.println("You give up. The details are left to your imagination.");
if (zombieApoc.getNumBullets() > 0)
{
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 1);
}
zombieApoc.setAlive();
//And here.
default:
System.out.println("This doesn't work if you don't pick one of the four valid options.");
}
}
System.out.println(zombieApoc);
//Why did it reach end of file while parsing?
//HINT: { or } turn yellow when you highlight them if they are paired with another { or }
}
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/**
*This is the domain class for a lab meant to test error-checking.
*
* Now where did I leave my comments...? Oh well, guess you'll have to figure it out.
* @author Kevin Criado
*/
class ErrorDomain {
private double numZombies;
private double numBullets;
private boolean alive;
public ErrorDomain(int aZombies, int someBullets){
numZombies = aZombies;
numBullets = someBullets;
alive = true;
}
//What variable type are we trying to return?
//HINT: It's not String
//return type is changed from string to double since the variable numZombies is of double data type
public double getNumZombies()
{
return numZombies;
}
//What variable type are we trying to return?
//return type is changed from boolean to double since the variable numBullets is of double data type
public double getNumBullets()
{
return numBullets;
}
public boolean getAlive()
{
return alive;
}
public void setNumZombies(double zombies)
{
//What exactly does possible lossy conversion mean?
//Think about it. If we turn 3.26564548112 into an Integer we get: 3
//3 isn't the same as 3.26564548112
//changed the data type of numZombies to double to keep the precision
numZombies = zombies;
}
public void setNumBullets(double bullets)
{
//See setNumZombies(), this is a similar problem.
//changed the data type of numBullets to double to keep the precision
numBullets = bullets;
}
public void setAlive()
{
//This is an easy one. Fix it!
//put proper semicolon to end the line in java
alive = !alive;
}
public String toString()
{
String str = "There are currently " + numZombies + " zombies, and you have " + numBullets + " bullets left.";
if (!alive)
{
str += " Unfortunately, you are dead.";
}
if (numZombies == 0)
{
str += " Congratulations on surviving the zombie apocalypse!";
}
return str;
}
}
/**
* This is the driver for a class that is meant for practice on error checking.
*
* I think I misplaced all my comments. Darn. Good luck and have fun!
* @author Kevin Criado, and Matt messed it up.
*/
public class ErrorTester {
public static ErrorDomain zombieApoc;
public static Random helpMe = new Random();
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//We are trying to call non-static methods from a static method, how do we fix this?
//change the method beginApocalypse to static since the static method can only call static method from them
beginApocalypse();
//change the method survive to static since the static method can only call static method from them
survive();
}
public static void beginApocalypse()
{
System.out.println("SUDDENLY, ZOMBIES!");
int zombies = helpMe.nextInt(30)+5;
int bullets = helpMe.nextInt(10)+10;
zombieApoc = new ErrorDomain(zombies, bullets);
}
public static void survive()
{
Scanner kb = new Scanner(System.in);
while (zombieApoc.getAlive() && zombieApoc.getNumZombies() != 0)
{
System.out.println(zombieApoc);
System.out.println("What do you do?");
System.out.println("1. Shoot a zombie.");
System.out.println("2. Look for more bullets.");
System.out.println("3. Kill all remaining zombies.");
System.out.println("4. Give up.");
int choice = kb.nextInt();
//For some reason every case below the number you selected executes.
//I think our switch statement is broken, or maybe it's just missing something.
switch (choice)
{
//We are expecting a 1 here but this doesn't work. How can I fix this?
// since we are pass int value it should be without codes
case 1:
if (zombieApoc.getNumBullets() > 0)
{
System.out.println("You shot a zombie successfully!");
zombieApoc.setNumZombies(zombieApoc.getNumZombies() - 1);
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 1);
}
else
{
System.out.println("You are out of bullets! Why didn't you think this through?!");
zombieApoc.setAlive();
}
//I think we are missing something here.
// since we are pass int value it should be without codes
case 2:
int successCheck = helpMe.nextInt(40);
if (successCheck > zombieApoc.getNumZombies())
{
int moreBullets = helpMe.nextInt(3)+1;
zombieApoc.setNumBullets(zombieApoc.getNumBullets() + moreBullets);
System.out.println("You found some bullets! You now have " + zombieApoc.getNumBullets() + " bullets.");
}
else if (zombieApoc.getNumBullets() > 1)
{
System.out.println("Oh no! More zombies! You killed one but wasted a bullet in panic!");
zombieApoc.setNumZombies(zombieApoc.getNumZombies() - 1);
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 2);
}
else
{
System.out.println("Oh no! Zombies! And you're out of bullets! Aaaaaaaa!");
zombieApoc.setAlive();
}
//And here.
// since we are pass int value it should be without codes
case 3:
if (zombieApoc.getNumBullets() >= zombieApoc.getNumZombies())
{
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - zombieApoc.getNumZombies());
zombieApoc.setNumZombies(0);
System.out.println("You successfully take out all remaining zombies.");
}
else
{
System.out.println("You need one bullet for every zombie still left to use this command.");
}
//And here.
case 4:
System.out.println("You give up. The details are left to your imagination.");
if (zombieApoc.getNumBullets() > 0)
{
zombieApoc.setNumBullets(zombieApoc.getNumBullets() - 1);
}
zombieApoc.setAlive();
//And here.
default:
System.out.println("This doesn't work if you don't pick one of the four valid options.");
}
}
System.out.println(zombieApoc);
//Why did it reach end of file while parsing?
//HINT: { or } turn yellow when you highlight them if they are paired with another { or }
//adding a curly braces to match the starting braces
}
}
----------------------output-----------------------------
SUDDENLY, ZOMBIES!
There are currently 15.0 zombies, and you have 11.0 bullets left.
What do you do?
1. Shoot a zombie.
2. Look for more bullets.
3. Kill all remaining zombies.
4. Give up.
1
You shot a zombie successfully!
You found some bullets! You now have 11.0 bullets.
You need one bullet for every zombie still left to use this command.
You give up. The details are left to your imagination.
This doesn't work if you don't pick one of the four valid options.
There are currently 14.0 zombies, and you have 10.0 bullets left.
Unfortunately, you are dead.
SUDDENLY, ZOMBIES!
There are currently 26.0 zombies, and you have 17.0 bullets left.
What do you do?
1. Shoot a zombie.
2. Look for more bullets.
3. Kill all remaining zombies.
4. Give up.
2
Oh no! More zombies! You killed one but wasted a bullet in panic!
You need one bullet for every zombie still left to use this command.
You give up. The details are left to your imagination.
This doesn't work if you don't pick one of the four valid options.
There are currently 25.0 zombies, and you have 14.0 bullets left.
Unfortunately, you are dead.
//NOte: all the changes are made in the comments. Please go through it. and let me know incase of any doubts. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.