I've used to site for many commands I had trouble using and it was very helpful. I thought you guys might be able to help me out and help me understand what i'm doing wrong here. The "Rabbit"/"R" is moving as I wanted, however the old position is not resetting its old value from the copied array. Logic for winning and losing aren't debugged, yet so ignore that.
EDIT*
I can't shorten the code due to the relations between the array has with the program. The precise description for the error is when the "Rabbit"/"R" moves from (Using representative values) GridCompOne[5][5] to GridCompOne[4][5] going "up" on the "grid" I have a second array called GridCompTwo which is a copied version of the first grid when the random values were generated. What is supposed to happen is the Rabbit's value, which = 4 is supposed to replace GridCompOne[4][5] to = 4 and the old position GridCompOne[5][5] is supposed to be replaced with GridCompTwo[5][5] old value being 3 which is supposed to represent the ground value I have assigned. But the replacing of the value doesn't seem to work.
import java.util.Random;
import java.util.Scanner;
public class SurvivalGameVersionTwo
{
private int Repeat;
public int WinLose = (1);
private int Turn = (1);
public static void main(String[] args)
{
String User;
int Repeat = (0);
//Creating Objects
SurvivalGameVersionTwo game = new SurvivalGameVersionTwo();
Score score = new Score();
Grid grid = new Grid();
Spawner spawner = new Spawner();
Display display = new Display();
User user = new User();
Time time = new Time();
Logic logic = new Logic();
Movement movement = new Movement();
//Calling object's class methods
Intro();
User = GetUser();
user.setName(User);
while (Repeat > 2);
{
game.Ready();
grid.RandomGridGenerator();
spawner.RabbitWolfSpawner();
movement.Copy();
while (game.WinLose <= 2)
{
display.UserGrid();
game.Turn = Turns(game.Turn);
movement.Movement();
game.WinLose = logic.WinLose(game.Turn);
time.Delay();
}
display.UserGrid();
Repeat = Repeat();
game.WinLose = (1);
}
}
public static void Intro()
{
System.out.printf("Welcome to the Rabbit Survival Game!\n");
System.out.println("Rules to win: Rabbit must cross the bridge.");
System.out.println("Rules to lose: Rabbir drowns in the water, \n"+
"eaten by the wolf, or starve to death in 30 turns.");
System.out.println("------------------------------------------");
}
public static String GetUser()
{
Scanner Input = new Scanner (System.in);
String Name;
System.out.println("Please type in your name please.");
Name = Input.next();
return Name;
}
public static void Ready()
{
Scanner Input = new Scanner (System.in);
String Ready;
String Yes = ("Yes");
String No = ("No");
System.out.println("Are you ready to play?");
System.out.println("Type in 'Yes', or 'No' to continue.");
Ready = Input.next();
if (Ready.equals(Yes) == true)
{
System.out.println("Lets begin!!!");
}
else if (Ready.equals(No) == true)
{
System.out.println("Goodbye, and hope you come back to play again.");
System.exit(0);
}
else
{
System.out.println("There was an error!!! Restart the program to try again.");
System.exit(0);
}
}
public static int Turns(int turns)
{
int Turns;
System.out.printf("Turn %d.",turns);
Turns = (turns + 1);
return Turns;
}
public static int Repeat()
{
Scanner Input = new Scanner (System.in);
int repeat;
System.out.println("Do you wish to play again?");
repeat = Input.nextInt();
return repeat;
}
}
class User
{
private String Name;
public void UserInfo(String name)
{
this.Name=name;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
class Score
{
public static int Total = 0;
public static int Win = 0;
public static int Lose = 0;
public static double Ratio = 0;
}
class Grid {
public static int[][] GridCompOne = new int[10][10];
public static int[] Rabbit = new int[2];
public static int[] Wolf = new int[2];
public static void RandomGridGenerator() {
Random r = new Random();
int i = 1;
int j = 1;
int k = 0;
int Low = 1;
int High = 3;
int Random;
//parameters for bridge and water spawning
while (k <= 9) {
Random = r.nextInt(High - Low) + Low;
GridCompOne[0][k] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[k][0] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[9][k] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[k][9] = Random;
k++;
}
while (i <= 8) {
while (j <= 8) {
GridCompOne[i][j] = 3;
j++;
}
j = 1;
i++;
}
}
}
class Spawner extends Grid
{
public static void RabbitWolfSpawner()
{
Random r = new Random();
int Low = 1;
int High = 8;
int XAxis;
int YAxis;
int Random;
//Rabbit
Random = r.nextInt(High - Low) + Low;
XAxis = Random;
Random = r.nextInt(High - Low) + Low;
YAxis = Random;
GridCompOne[XAxis][YAxis] = 4;
Rabbit[0] = XAxis;
Rabbit[1] = YAxis;
//Wolf
Random = r.nextInt(High - Low) + Low;
XAxis = Random;
Random = r.nextInt(High - Low) + Low;
YAxis = Random;
GridCompOne[XAxis][YAxis] = 5;
Wolf[0] = XAxis;
Wolf[1] = YAxis;
}
}
class Movement extends Grid
{
private static int[][] GridCompTwo = new int[10][10];
public static void Copy()
{
System.arraycopy(GridCompOne, 0, GridCompTwo, 0, GridCompOne.length);
}
//Rabbit and Wolf Movement
public static void Movement()
{
int High = 4;
int Low = 1;
Random R = new Random();
int Random = R.nextInt(High - Low) + Low;
//Rabbit
switch (Random)
{
case 1:
{
//Up
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0] - 1][Rabbit[1]] = 4;
Rabbit[0] = (Rabbit[0] - 1);
break;
}
case 2:
{
//Down
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0] + 1][Rabbit[1]] = 4;
Rabbit[0] = (Rabbit[0] + 1);
break;
}
case 3:
{
//Left
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0]][Rabbit[1] - 1] = 4;
Rabbit[1] = (Rabbit[1] - 1);
break;
}
case 4:
{
//Right
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0]][Rabbit[1] + 1] = 4;
Rabbit[1] = (Rabbit[1] + 1);
break;
}
default:
{
System.out.println("There was an error!!!");
System.exit(0);
}
}
Random = R.nextInt(High - Low) + Low;
//Wolf
switch (Random)
{
case 1: {
//Up
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0] - 1][Wolf[1]] = 5;
Wolf[0] = (Wolf[0] - 1);
break;
}
case 2: {
//Down
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0] + 1][Wolf[1]] = 5;
Wolf[0] = (Wolf[0] + 1);
break;
}
case 3: {
//Left
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0]][Wolf[1] - 1] = 5;
Wolf[1] = (Wolf[1] - 1);
break;
}
case 4: {
//Right
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0]][Wolf[1] + 1] = 5;
Wolf[1] = (Wolf[1] + 1);
break;
}
default: {
System.out.println("There was an error!!!");
System.exit(0);
}
}
}
}
class Display extends Movement
{
private static char[][] GridUser = new char[10][10];
public static void UserGrid()
{
int i = 0;
int j = 0;
char Letter = ' ';
System.out.println("---------------------");
while (i <= 9)
{
while (j <= 9)
{
System.out.print("|");
switch (GridCompOne[i][j])
{
case 1:
{
//Water
Letter = '~';
break;
}
case 2:
{
//Bridge
Letter = '=';
break;
}
case 3:
{
//Ground
Letter = ',';
break;
}
case 4:
{
//Rabbit
Letter = 'R';
break;
}
case 5:
{
//Wolf
Letter = 'W';
break;
}
default:
{
//Error!!!
System.out.println("There was an error in the program!!!");
System.exit(0);
}
}
GridUser[i][j] = Letter;
System.out.print(GridUser[i][j]);
j++;
}
System.out.print("|");
System.out.println();
System.out.println("---------------------");
j = 0;
i++;
}
}
}
class Logic extends Grid
{
public static int WinLose(int counter)
{
int winlose = (0);
if(GridCompOne[Rabbit[0]][Rabbit[1]]==1)
{
System.out.println("The Rabbit has drowned!");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else if(GridCompOne[Rabbit[0]][Rabbit[1]]==2)
{
System.out.println("The Rabbit has Escaped!");
System.out.println("Game over!");
Score.Win = Score.Win + 1;
winlose = 3;
}
else if(GridCompOne[Rabbit[0]][Rabbit[1]]==5)
{
System.out.println("The Rabbit has been Eaten by the Wolf!");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else if(counter>30)
{
System.out.println("The Rabbit has starved to death");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else
{
System.out.println(" (Next Turn in a couple seconds)");
}
return winlose;
}
}
class Time
{
public static void Delay()
{
try
{
long Seconds = (2000);
Thread.sleep(Seconds);
}
catch (InterruptedException e)
{
System.out.println("...");
}
}
}
The root cause for the problem is System.arraycopy. As of my understanding it is copying reference instead of values (may be i am wrong).
i changed to manual copy then it is working fine up to the logic of replacing array. Please check below modified code with my comments
import java.util.Random;
import java.util.Scanner;
public class SurvivalGameVersionTwo
{
private int Repeat;
public int WinLose = (1);
private int Turn = (1);
public static void main(String[] args)
{
String User;
int Repeat = (3);//changed: to enter into while loop Repeat should be more than 2
//Creating Objects
SurvivalGameVersionTwo game = new SurvivalGameVersionTwo();
Score score = new Score();
Grid grid = new Grid();
Spawner spawner = new Spawner();
Display display = new Display();
User user = new User();
Time time = new Time();
Logic logic = new Logic();
Movement movement = new Movement();
//Calling object's class methods
Intro();
User = GetUser();
user.setName(User);
while (Repeat > 2)//changed: removed ;
{
game.Ready();
grid.RandomGridGenerator();
movement.Copy();//changed:First copy then change GridCompOne array
spawner.RabbitWolfSpawner();
while (game.WinLose <= 2)
{
display.UserGrid();
game.Turn = Turns(game.Turn);
movement.Movement();
game.WinLose = logic.WinLose(game.Turn);
time.Delay();
}
display.UserGrid();
Repeat = Repeat();
game.WinLose = (1);
}
}
public static void Intro()
{
System.out.printf("Welcome to the Rabbit Survival Game!\n");
System.out.println("Rules to win: Rabbit must cross the bridge.");
System.out.println("Rules to lose: Rabbir drowns in the water, \n"+
"eaten by the wolf, or starve to death in 30 turns.");
System.out.println("------------------------------------------");
}
public static String GetUser()
{
Scanner Input = new Scanner (System.in);
String Name;
System.out.println("Please type in your name please.");
Name = Input.next();
return Name;
}
public static void Ready()
{
Scanner Input = new Scanner (System.in);
String Ready;
String Yes = ("Yes");
String No = ("No");
System.out.println("Are you ready to play?");
System.out.println("Type in 'Yes', or 'No' to continue.");
Ready = Input.next();
if (Ready.equals(Yes) == true)
{
System.out.println("Lets begin!!!");
}
else if (Ready.equals(No) == true)
{
System.out.println("Goodbye, and hope you come back to play again.");
System.exit(0);
}
else
{
System.out.println("There was an error!!! Restart the program to try again.");
System.exit(0);
}
}
public static int Turns(int turns)
{
int Turns;
System.out.printf("Turn %d.",turns);
Turns = (turns + 1);
return Turns;
}
public static int Repeat()
{
Scanner Input = new Scanner (System.in);
int repeat;
System.out.println("Do you wish to play again?");
repeat = Input.nextInt();
return repeat;
}
}
class User
{
private String Name;
public void UserInfo(String name)
{
this.Name=name;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
class Score
{
public static int Total = 0;
public static int Win = 0;
public static int Lose = 0;
public static double Ratio = 0;
}
class Grid {
public static int[][] GridCompOne = new int[10][10];
public static int[] Rabbit = new int[2];
public static int[] Wolf = new int[2];
public static void RandomGridGenerator() {
Random r = new Random();
int i = 1;
int j = 1;
int k = 0;
int Low = 1;
int High = 3;
int Random;
//parameters for bridge and water spawning
while (k <= 9) {
Random = r.nextInt(High - Low) + Low;
GridCompOne[0][k] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[k][0] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[9][k] = Random;
Random = r.nextInt(High - Low) + Low;
GridCompOne[k][9] = Random;
k++;
}
while (i <= 8) {
while (j <= 8) {
GridCompOne[i][j] = 3;
j++;
}
j = 1;
i++;
}
}
}
class Spawner extends Grid
{
public static void RabbitWolfSpawner()
{
Random r = new Random();
int Low = 1;
int High = 8;
int XAxis;
int YAxis;
int Random;
//Rabbit
Random = r.nextInt(High - Low) + Low;
XAxis = Random;
Random = r.nextInt(High - Low) + Low;
YAxis = Random;
GridCompOne[XAxis][YAxis] = 4;
Rabbit[0] = XAxis;
Rabbit[1] = YAxis;
//Wolf
Random = r.nextInt(High - Low) + Low;
XAxis = Random;
Random = r.nextInt(High - Low) + Low;
YAxis = Random;
GridCompOne[XAxis][YAxis] = 5;
Wolf[0] = XAxis;
Wolf[1] = YAxis;
}
}
class Movement extends Grid
{
private static int[][] GridCompTwo = new int[10][10];
public static void Copy()
{
//System.arraycopy(GridCompOne, 0, GridCompTwo, 0, GridCompOne.length);
//changed
//As of my understanding System.arraycopy is not coping values , it is copying references
for(int i=0;i<GridCompOne.length;i++){
for(int j=0;j<GridCompOne[i].length;j++){
GridCompTwo[i][j]=GridCompOne[i][j];
}
}
}
//Rabbit and Wolf Movement
public static void Movement()
{
int High = 4;
int Low = 1;
Random R = new Random();
int Random = R.nextInt(High - Low) + Low;
//Rabbit
switch (Random)
{
case 1:
{
//Up
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0] - 1][Rabbit[1]] = 4;
Rabbit[0] = (Rabbit[0] - 1);
break;
}
case 2:
{
//Down
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0] + 1][Rabbit[1]] = 4;
Rabbit[0] = (Rabbit[0] + 1);
break;
}
case 3:
{
//Left
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0]][Rabbit[1] - 1] = 4;
Rabbit[1] = (Rabbit[1] - 1);
break;
}
case 4:
{
//Right
GridCompOne[Rabbit[0]][Rabbit[1]] = GridCompTwo[Rabbit[0]][Rabbit[1]];
GridCompOne[Rabbit[0]][Rabbit[1] + 1] = 4;
Rabbit[1] = (Rabbit[1] + 1);
break;
}
default:
{
System.out.println("There was an error!!!");
System.exit(0);
}
}
Random = R.nextInt(High - Low) + Low;
//Wolf
switch (Random)
{
case 1: {
//Up
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0] - 1][Wolf[1]] = 5;
Wolf[0] = (Wolf[0] - 1);
break;
}
case 2: {
//Down
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0] + 1][Wolf[1]] = 5;
Wolf[0] = (Wolf[0] + 1);
break;
}
case 3: {
//Left
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0]][Wolf[1] - 1] = 5;
Wolf[1] = (Wolf[1] - 1);
break;
}
case 4: {
//Right
GridCompOne[Wolf[0]][Wolf[1]] = GridCompTwo[Wolf[0]][Wolf[1]];
GridCompOne[Wolf[0]][Wolf[1] + 1] = 5;
Wolf[1] = (Wolf[1] + 1);
break;
}
default: {
System.out.println("There was an error!!!");
System.exit(0);
}
}
}
}
class Display extends Movement
{
private static char[][] GridUser = new char[10][10];
public static void UserGrid()
{
int i = 0;
int j = 0;
char Letter = ' ';
System.out.println("---------------------");
while (i <= 9)
{
while (j <= 9)
{
System.out.print("|");
switch (GridCompOne[i][j])
{
case 1:
{
//Water
Letter = '~';
break;
}
case 2:
{
//Bridge
Letter = '=';
break;
}
case 3:
{
//Ground
Letter = ',';
break;
}
case 4:
{
//Rabbit
Letter = 'R';
break;
}
case 5:
{
//Wolf
Letter = 'W';
break;
}
default:
{
//Error!!!
System.out.println("There was an error in the program!!!");
System.exit(0);
}
}
GridUser[i][j] = Letter;
System.out.print(GridUser[i][j]);
j++;
}
System.out.print("|");
System.out.println();
System.out.println("---------------------");
j = 0;
i++;
}
}
}
class Logic extends Grid
{
public static int WinLose(int counter)
{
int winlose = (0);
if(GridCompOne[Rabbit[0]][Rabbit[1]]==1)
{
System.out.println("The Rabbit has drowned!");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else if(GridCompOne[Rabbit[0]][Rabbit[1]]==2)
{
System.out.println("The Rabbit has Escaped!");
System.out.println("Game over!");
Score.Win = Score.Win + 1;
winlose = 3;
}
else if(GridCompOne[Rabbit[0]][Rabbit[1]]==5)
{
System.out.println("The Rabbit has been Eaten by the Wolf!");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else if(counter>30)
{
System.out.println("The Rabbit has starved to death");
System.out.println("Game over!");
Score.Lose = Score.Lose + 1;
winlose = 3;
}
else
{
System.out.println(" (Next Turn in a couple seconds)");
}
return winlose;
}
}
class Time
{
public static void Delay()
{
try
{
long Seconds = (2000);
Thread.sleep(Seconds);
}
catch (InterruptedException e)
{
System.out.println("...");
}
}
}
Thanks
Related
Good day, I have the following assignment:
https://prnt.sc/113pc7j
My problem is the first assignment of the fourth item. I prescribed filling the array with random objects, however, when I want to display this array on the screen, I get different results all the time.
My code:
The interface itself:
interface VehicleAndWorkers
{
String cars();
}
First class that implements the interface:
public class TaxiStation implements VehicleAndWorkers{
TrucksLogistic trucks = new TrucksLogistic();
public String[] cars = {"KIA", "Hyundai", "Volkswagen", "Lada", "Datsun", "Skoda"};
int quantCars = 150;
int workers = 300;
public String cars() {
System.out.println("List of models of our taxi fleet: ");
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
return null;
}
#Override
public boolean equals(Object obj)
{
return trucks.quantTrucks == this.quantCars;
}
#Override
public int hashCode() {
return Objects.hash(trucks, quantCars);
}
#Override
public String toString() {
return cars();
}
}
Second class that implements the interface:
public class TrucksLogistic implements VehicleAndWorkers {
String[] trucks = {"Kenworth W900", "Volvo FH16", "Scania R730", "MAN TGS 18.400", "МАЗ 6430",
"КАМАЗ-5490 НЕО 2"};
int quantTrucks = 40;
int workers = 100;
public String cars()
{
System.out.println("Our company model list: ");
for (int i = 0; i < trucks.length; i++)
{
System.out.println(trucks[i]);
}
System.out.println("Amount of workers: " + workers);
System.out.println("Number of trucks: " + quantTrucks);
System.out.println("The ratio of the number of truck models to the number of trucks: " +
Math.round(trucks.length / quantTrucks));
return null;
}
#Override
public String toString() {
return cars();
}
}
The class in which I tried to implement working with ArrayList (filling, displaying, etc.):
public class InterfaceArray {
ArrayList<VehicleAndWorkers> array = new ArrayList<>();
public void setArray() {
int randomLength = (int) Math.floor(random() * 10);
for (int i = 0; i < randomLength; i++) {
Random random = new Random();
int minRandomNum = 1;
int maxRandomNum = 2;
int diffRandomNum = maxRandomNum - minRandomNum;
int randomNum = random.nextInt(diffRandomNum + 1) + minRandomNum;
if (randomNum == 1) {
array.add(new TrucksLogistic() {
#Override
public String cars() {
int num = (int) Math.floor(random() * trucks.length);
quantTrucks = (int) Math.floor(random() * 100);
Random random = new Random();
int minWorkers = quantTrucks;
int maxWorkers = 200;
int diffWorkers = maxWorkers - minWorkers;
workers = random.nextInt(diffWorkers + 1);
workers += minWorkers;
System.out.println("Number of trucks: " + quantTrucks);
System.out.println("Amount of workers: " + workers);
System.out.println("Average number of workers per truck: " + Math.round(workers /
quantTrucks));
return "Priority truck brand/model: " + trucks[num];
}
});
} else {
array.add(new TaxiStation() {
#Override
public String cars() {
int num = (int) Math.floor(random() * cars.length);
quantCars = (int) Math.floor(random() * 100);
Random random = new Random();
int minWorkers = quantCars;
int maxWorkers = 200;
int diffWorkers = maxWorkers - minWorkers;
workers = random.nextInt(diffWorkers + 1);
workers += minWorkers;
System.out.println("Number of passenger cars: " + quantCars);
System.out.println("Amount of workers: " + workers);
System.out.println("Average number of employees per vehicle: " + Math.round(workers / quantCars));
return "Priority car brand/model:" + cars[num];
}
});
}
}
}
public void getArray() {
for (VehicleAndWorkers element : array) {
System.out.println("");
System.out.println(element);
System.out.println("");
}
}
public void sameElements() {
//// Set<VehicleAndWorkers> arrayTwo = new LinkedHashSet<>(array);
//// for (VehicleAndWorkers element : arrayTwo)
//// System.out.println(element + "\n");
//
// for (int element = 0; element < array.size() - 1; element++)
// {
// for (int i = element + 1; i < array.size(); element++)
// {
// if (array.get(element).equals(element + 1))
// {
// arrayTwo.add(array.get(element + 1));
// }
// }
// array.remove(array.get(element));
// }
// for (VehicleAndWorkers element : arrayTwo)
// System.out.println(element.toString());
}
public void sameTypeElements() {
ArrayList<VehicleAndWorkers> arrayTwo = new ArrayList<>();
// for (VehicleAndWorkers element : array)
// {
// if (element)
// }
}
}
Well, the class in which the methods from the previous class are applied:
public class ConsoleInterface {
void doChoice()
{
InterfaceArray interfaceArray = new InterfaceArray();
printOptions();
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
while (choice >= 0 && choice < 5) {
if (choice == 0) {
interfaceArray.setArray();
interfaceArray.getArray();
printOptions();
choice = scan.nextInt();
}
else if (choice == 1) {
interfaceArray.setArray();
printOptions();
choice = scan.nextInt();
}
else if (choice == 2) {
interfaceArray.sameElements();
printOptions();
choice = scan.nextInt();
}
else if (choice == 3) {
interfaceArray.sameTypeElements();
printOptions();
choice = scan.nextInt();
}
else if (choice == 4) {
interfaceArray.getArray();
printOptions();
choice = scan.nextInt();
}
}
}
void printOptions()
{
System.out.println("Choose an action: \n");
System.out.println("0 - Fill the array with random elements and display it on the screen. \n");
System.out.println("1 - Fill the array with random elements. \n");
System.out.println("2 - Find objects in the array,\n" +
" whose functional method returns the same result. \n");
System.out.println("3 - Split the original array into two arrays, \n" +
" which will store the same type of elements. \n");
System.out.println("4 - Display the array(-s) to the screen.\n");
System.out.println("Any key - Exit the application.\n");
}
}
An example of how the program works:
I fill the array and immediately output it to the console: https://prnt.sc/113qmkr
I get the result:
https://prnt.sc/113qurt
https://prnt.sc/113qvaq
I try again to display all the objects in the array: https://prnt.sc/113qvz4
And... I get completely different results:
https://prnt.sc/113qwhx
https://prnt.sc/113qwps
Moreover, as you may have noticed, the length of the array is preserved, but the objects themselves are already different.
Honestly, I don't know what the problem might be, the teacher at the university also threw up his hands, so I will be glad to any criticism, comments and suggestions. Thank you in advance.
Heading
Program for Java class, supposed to be the game Pass the Pigs, tried everything, but cant get the special case marked by //FIXME to execute. Below is the code i have written. Any advice welcome on where i went wrong. I know there is a way to make it shorter using a for-loop, but i need to get this program working properly before trying to get it looking sleek. Thank you.
package ch07labx;
import java.util.Random;
import java.util.Scanner;
public class PassThePigs {
//names of pieces
public static final String SIDER_NO_DOT = "Sider(no dot)";
public static final String SIDER_DOT = "Sider(dot)";
public static final String TROTTER = "Trotter";
public static final String RAZORBACK = "Razorback";
public static final String SNOUTER = "Snouter";
public static final String LEANING_JOWLER = "Leaning Jowler";
//score for piece landings
public static final int SIDER_NO_DOT_SCORE = 0;
public static final int SIDER_DOT_SCORE = 0;
public static final int TROTTER_SCORE = 5;
public static final int RAZORBACK_SCORE = 5;
public static final int SNOUTER_SCORE = 10;
public static final int LEANING_JOWLER_SCORE = 15;
//odds of landing
public static final int SIDER_NO_DOT_PROB = 33;
public static final int SIDER_DOT_PROB = 66;
public static final int TROTTER_PROB = 76;
public static final int RAZORBACK_PROB = 96;
public static final int SNOUTER_PROB = 99;
public static final int LEANING_JOWLER_PROB = 100;
private static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) {
String pigRollOne = null;
String pigRollTwo = null;
int wholeTurnScore = 0;
String userInput = null;
do {
int rollScore = 0;
Random randGen = new Random();
int rollOne = randGen.nextInt(100) + 1;
int rollTwo = randGen.nextInt(100) + 1;
int rollOneScore = 0;
int rollTwoScore = 0;
//rollOne outcomes
if (rollOne <= SIDER_NO_DOT_PROB) {
pigRollOne = SIDER_NO_DOT;
rollOneScore = SIDER_NO_DOT_SCORE;
} else if (rollOne <= SIDER_DOT_PROB) {
pigRollOne = SIDER_DOT;
rollOneScore = SIDER_DOT_SCORE;
} else if (rollOne <= TROTTER_PROB) {
pigRollOne = TROTTER;
rollOneScore = TROTTER_SCORE;
} else if (rollOne <= RAZORBACK_PROB) {
pigRollOne = RAZORBACK;
rollOneScore = RAZORBACK_SCORE;
} else if (rollOne <= SNOUTER_PROB) {
pigRollOne = SNOUTER;
rollOneScore = SNOUTER_SCORE;
} else if (rollOne == LEANING_JOWLER_PROB) {
pigRollOne = LEANING_JOWLER;
rollOneScore = LEANING_JOWLER_SCORE;
}
//rollTwo outcomes
if (rollTwo <= SIDER_NO_DOT_PROB) {
pigRollTwo = SIDER_NO_DOT;
rollTwoScore = SIDER_NO_DOT_SCORE;
} else if (rollTwo <= SIDER_DOT_PROB) {
pigRollTwo = SIDER_DOT;
rollTwoScore = SIDER_DOT_SCORE;
} else if (rollTwo <= TROTTER_PROB) {
pigRollTwo = TROTTER;
rollTwoScore = TROTTER_SCORE;
} else if (rollTwo <= RAZORBACK_PROB) {
pigRollTwo = RAZORBACK;
rollTwoScore = RAZORBACK_SCORE;
} else if (rollTwo <= SNOUTER_PROB) {
pigRollTwo = SNOUTER;
rollTwoScore = SNOUTER_SCORE;
} else if (rollTwo == LEANING_JOWLER_PROB) {
pigRollTwo = LEANING_JOWLER;
rollTwoScore = LEANING_JOWLER_SCORE;
}
rollScore = rollOneScore + rollTwoScore;
//special cases
if ((pigRollOne.equals(SIDER_DOT) && pigRollTwo.equals(SIDER_NO_DOT)) ||
(pigRollOne.equals(SIDER_NO_DOT) && pigRollTwo.equals(SIDER_DOT))){
rollScore = 0;
wholeTurnScore = 0;
System.out.print("Rolling... " + pigRollOne +
" & " + pigRollTwo);
System.out.println();
System.out.print("this roll: " + rollScore);
System.out.println();
System.out.print("this turn: " + wholeTurnScore);
System.out.println();
System.out.println("Pig out!");
break;
}
if ((pigRollOne.equals(SIDER_DOT) && pigRollTwo.equals(SIDER_DOT)) ||
(pigRollOne.equals(SIDER_NO_DOT) && pigRollTwo.equals(SIDER_NO_DOT))){
rollScore = 1; //FIXME
}
if (pigRollOne.equals(pigRollTwo)) {
rollScore = 2 * (rollOneScore + rollTwoScore);
}
wholeTurnScore += rollScore;
//screen outputs
System.out.print("Rolling... " + pigRollOne +
" & " + pigRollTwo);
System.out.println();
System.out.print("this roll: " + rollScore);
System.out.println();
System.out.print("this turn: " + wholeTurnScore);
System.out.println();
System.out.print("Roll again (y/n)? ");
userInput = scnr.next();
if(userInput.charAt(0) == 'n'){
break;
}
}while (userInput.charAt(0) == 'y');
}
}
I'm creating an UNO project and am a bit confused about how to assign the drivers to my DiscardPile class.
Thank you in advance for any answers you may give.
Sincerely,
Jayda M
I've asked my fellow students for assistance and have scoured the internet for anything that could be helpful with no luck. My professor isn't helpful at all haha
This is the full code for the Driver.java file. If you need me to give you any of the others I have no problem doing so.
package unoGame;
import java.util.Random;
//import java.util.Collections;
public class Driver {
public static unoDeck theDeck = new unoDeck();
public static PlayerHand[] thePlayers;
public static DiscardPile dp = new DiscardPile();
public static int nextPlayer;
public static Random getRandom = new Random();
public static void main(String[] args) {
// TODO Auto-generated method stub
thePlayers = new PlayerHand[3];
thePlayers[0] = new PlayerHand("name 1");
thePlayers[1] = new PlayerHand("name 2");
thePlayers[2] = new PlayerHand("name 3");
theDeck.shuffle();
System.out.println("Here is the Deck: " + theDeck);
System.out.println(" Welcome to UNO! ");
if (dp.isEmpty()) {
System.out.println(" Discard is Empty ");
}
else {
System.out.println(" Not Empty ");
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < thePlayers.length; j++) {
thePlayers[j].addCard(theDeck.deal());
}
}
nextPlayer = 0;
showPlayers();
dp.addCard(theDeck.deal());
while (checkForWin() == false) {
findNextPlayer();
if (theDeck.isEmpty()) {
theDeck.replenish(dp.clear());
}
playerTurn();
}
System.out.println(thePlayers[nextPlayer], getName() + " Wins!")
}
public static void showPlayers() {
for (PlayerHand p : thePlayers)
System.out.println(p);
}
public static boolean checkForWin() {
if (p.isWin()) {
win = true;
}
return win;
}
public static void findNextPlayer() {
nextPlayer++;
nextPlayer = nextPlayer % thePlayers.length;
if(dp.getTopCard().getValue()=="SK") {
nextPlayer++;
nextPlayer = nextPlayer % thePlayers.length;
}
if(dp.getTopCard().getValue() == "D2") {
for(int i = 0; i <= 1; i++) {
thePlayers[nextPlayer].addCard(theDeck.deal());
}
}
if(dp.getTopCard().getValue() == "W4") {
for(int i = 0; i <= 3; i++) {
thePlayers[nextPlayer].addCard(theDeck.deal());
}
}
if(dp.getTopCard().getValue() == "RV") {
for(int i = 0; i < thePlayers.length / 2; i++) {
PlayerHand rev = thePlayers[i];
thePlayers[i] = thePlayers[thePlayers.length - i -1];
rev = thePlayers[thePlayers.length - i -1];
if(nextPlayer == 0) {
nextPlayer = 0;
}
if(nextPlayer == 2) {
nextPlayer = 1;
}
}
nextPlayer++;
}
}
public static void playerTurn() {
if (thePlayers[nextPlayer].hasMatch(dp.getTopCard())) {
System.out.println(thePlayers[nextPlayer].getName() + " has a match!");
unoCard c = thePlayers[nextPlayer].playCard(dp.getTopCard());
dp.addCard(c);
if(c.getValue().equals("W")) {
c.setColor(theDeck.newColor());
}
if(c.getValue().equals("W4")) {
c.setColor(theDeck.newColor());
}
System.out.println(thePlayers[nextPlayer].getName() + " played a: " dp.getTopCard());
System.out.println(thePlayers[nextPlayer]);
}
else {
unoCard c = theDeck.deal();
thePlayers[nextPlayer]c.addCard(c);
System.out.println(thePlayers[nextPlayer].getName() + " drew a: " + c);
}
}
}
There are errors are on these lines of code: 46, 50, 57, 58, 60, 106 and 111. Most of them are saying they're undefined, and I'm unsure of where to do so amongst my files. I'm expecting the Driver to run the game as a whole, so because it's gone wack I can't run the build properly. If you need more information to work with, I'll gladly give it to you!
EDIT:
The lines and/or words where the errors are taking place have asterisks around them
The error descriptions with their corresponding lines
The printed console error after running Driver. Hopefully this works for the Reproducible Example required :)
Here is your Driver class with compilation problems fixed. I suggest you compare it with your current code in order to see what I changed.
I also needed to add method replenish() in class unoDeck because it didn't exist and is called from class Driver. Right now that method does nothing. As I said, I only added it in order to fix the compilation error.
package unoGame;
import java.util.Random;
public class Driver {
public static unoDeck theDeck = new unoDeck();
public static PlayerHand[] thePlayers;
public static DiscardPile dp = new DiscardPile();
public static int nextPlayer;
public static Random getRandom = new Random();
public static void main(String[] args) {
thePlayers = new PlayerHand[3];
thePlayers[0] = new PlayerHand("name 1");
thePlayers[1] = new PlayerHand("name 2");
thePlayers[2] = new PlayerHand("name 3");
theDeck.shuffle();
System.out.println("Here is the Deck: " + theDeck);
System.out.println(" Welcome to UNO! ");
if (dp.isEmpty()) {
System.out.println(" Discard is Empty ");
}
else {
System.out.println(" Not Empty ");
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < thePlayers.length; j++) {
thePlayers[j].addCard(theDeck.deal());
}
}
nextPlayer = 0;
showPlayers();
dp.addCard(theDeck.deal());
while (checkForWin() == false) {
findNextPlayer();
if (theDeck.isEmpty()) {
theDeck.replenish(dp.clear());
}
playerTurn();
}
System.out.println(thePlayers[nextPlayer].getName() + " Wins!");
}
public static void showPlayers() {
for (PlayerHand p : thePlayers)
System.out.println(p);
}
public static boolean checkForWin() {
boolean win = false;
if (thePlayers[nextPlayer].isWin()) {
win = true;
}
return win;
}
public static void findNextPlayer() {
nextPlayer++;
nextPlayer = nextPlayer % thePlayers.length;
if (dp.getTopCard().getValue() == "SK") {
nextPlayer++;
nextPlayer = nextPlayer % thePlayers.length;
}
if (dp.getTopCard().getValue() == "D2") {
for (int i = 0; i <= 1; i++) {
thePlayers[nextPlayer].addCard(theDeck.deal());
}
}
if (dp.getTopCard().getValue() == "W4") {
for (int i = 0; i <= 3; i++) {
thePlayers[nextPlayer].addCard(theDeck.deal());
}
}
if (dp.getTopCard().getValue() == "RV") {
for (int i = 0; i < thePlayers.length / 2; i++) {
PlayerHand rev = thePlayers[i];
thePlayers[i] = thePlayers[thePlayers.length - i - 1];
rev = thePlayers[thePlayers.length - i - 1];
if (nextPlayer == 0) {
nextPlayer = 0;
}
if (nextPlayer == 2) {
nextPlayer = 1;
}
}
nextPlayer++;
}
}
public static void playerTurn() {
if (thePlayers[nextPlayer].hasMatch(dp.getTopCard())) {
System.out.println(thePlayers[nextPlayer].getName() + " has a match!");
unoCard c = thePlayers[nextPlayer].playCard(dp.getTopCard());
dp.addCard(c);
if(c.getValue().equals("W")) {
c.setColor(theDeck.newColor());
}
if(c.getValue().equals("W4")) {
c.setColor(theDeck.newColor());
}
System.out.println(thePlayers[nextPlayer].getName() + " played a: " + dp.getTopCard());
System.out.println(thePlayers[nextPlayer]);
}
else {
unoCard c = theDeck.deal();
thePlayers[nextPlayer].addCard(c);
System.out.println(thePlayers[nextPlayer].getName() + " drew a: " + c);
}
}
}
public abstract class Character{
public enum Type{ ROGUE, PALADIN, JACKIE_CHEN, SKELETON, GOBLIN, WIZARD}
private String name;
private int hitPoints;
private int strength;
private Weapon weapon;
//other attributes
//methods
public Character(Type characterType){
switch(characterType){
case ROGUE:
//set the attributes for a Rogue
name = "Rogue";
// TODO: set other attributes
hitPoints = 55;
strength = 8;
Weapon rogue = new Weapon("Short Sword", 1, 4);
break;
case PALADIN:
//set the attributes for a Rogue
name = "Paladin";
// TODO: set other attributes
hitPoints = 35;
strength = 14;
Weapon paladin = new Weapon("Long Sword",3,7);
break;
case JACKIE_CHEN:
name = "Jackie Chen";
hitPoints =45;
strength = 10;
Weapon jackie = new Weapon("Jump Kick",2, 6);
break;
case SKELETON:
name = "Skeleton";
hitPoints = 25;
strength = 3;
Weapon skeleton = new Weapon("Short Sword" ,1, 4);
break;
case GOBLIN:
name = "Goblin";
hitPoints = 25;
strength = 4;
Weapon goblin = new Weapon("Axe",2,6);
break;
case WIZARD:
name = "Wizard";
hitPoints = 40;
strength = 8;
Weapon wizard = new Weapon("Fire Blast", 4, 10);
break;
}
}
public String getName(){
return name;
}
public int getHitPoints(){
return hitPoints;
}
public int getStrength(){
return strength;
}
public void setStrength(int _strength){
this.strength =_strength;
}
public void setWeapon(Weapon _weapon){
this.weapon = _weapon;
}
public void attack(){
}
public void increaseHitPoints(){
}
public void decreaseHitPoints(){
}
/*public boolean isDefeated(){
}*/
}
import java.util.Scanner;
import java.util.Random;
public class Player extends Character{
// attributes for the plauer class
// initilizes the fields
private int coins;
private Potion[] inventory;
Random randomNums = new Random();
Scanner keyboard = new Scanner(System.in);
//methods
public Player(Type playerType){
super(playerType);
coins = 0;
inventory = new Potion[5];
}
public void increaseStrength(int strengthIncrease){
enemy.getHitPoints() -= playerATK;
}
public int getCoins(){
return coins;
}
public void increaseCoins(int coins){
}
public void decreaseCoins(int coins){
}
public void addToInventory(String a, Type potion){
}
public void removeFromInventory(int index){
}
public void displayInventory(){
}
/*public int getNumOpenSlots(){
return numOpenSlots;
}*/
public void battleMinion(Enemy enemy){
Enemy goblin = new Enemy(Character.Type.GOBLIN);
int playerDamage =0, playerATK =0, enemyATK =0, enemyDamage =0;
if (enemy.getName() == "Goblin" && getName()=="Rogue"){
for (int i = 1; i <= goblin.getNumGoblins(); i++)
{
System.out.printf("***%s vs %s %d***\n", getName(), enemy.getName(), i);
while(enemy.getHitPoints() > 0 && getHitPoints() > 0)
{
playerDamage = randomNums.nextInt(Weapon.SHORT_SWORD_MAX - Weapon.SHORT_SWORD_MIN + 1) + Weapon.SHORT_SWORD_MIN;
playerATK = getStrength() + playerDamage;
enemy.getHitPoints() -= playerATK;
System.out.printf("%s attacks with ATK = %d + %d = %d\n", getName(), getStrength(), playerDamage, playerATK);
System.out.printf("%s HP is now %d - %d = %d\n\n", enemy.getName(), enemy.getHitPoints() + playerATK, playerATK, enemy.getHitPoints());
if (enemy.getHitPoints() <= 0)
break;
enemyDamage = randomNums.nextInt(Weapon.AXE_MAX - Weapon.AXE_MIN + 1) + Weapon.AXE_MAX;
enemyATK = enemy.getStrength() + enemyDamage;
getHitPoints() -= enemyATK;
System.out.printf("%s attacks with ATK = %d + %d = %d\n", getName(), enemy.getStrength(), enemyDamage, enemyATK);
System.out.printf("%s HP is now %d - %d = %d\n\n", getName(), getHitPoints() + enemyATK, enemyATK, getHitPoints());
} // end of while loop
if (getHitPoints() > 0){
System.out.printf("%s defeated %s %d!\n\n", getName(), enemy.getName(), i);
coins = randomNums.nextInt((50 - 30) + 1) + 30;
System.out.println(getName() + " gains " + coins + " gold coins!\n");
//player[4] += coins;
}
else
{
System.out.printf("--%s is defeated in battle!--\n\nGAME OVER\n", getName());
System.exit(0);
}
if(i <= goblin.getNumGoblins() - 1){
System.out.println("Press enter to continue...");
keyboard.nextLine();
}
}
}
Error is as follows
./Player.java:59: error: unexpected type
enemy.getHitPoints() -= playerATK;
^
required: variable
found: value
./Player.java:68: error: unexpected type
getHitPoints() -= enemyATK;
I know this is wrong, but what is the reason behind this? Also I have
public void increaseHitPoints(){
}
public void decreaseHitPoints(){
}
these two class in my character class, I don't how to code to make this work.
getHitPoints() is a method that returns a value. You can't assign anything to that expression, and the -= operator performs both subtraction and assignment.
Instead of
getHitPoints() -= playerATK;
write
setHitPoints (getHitPoints() - playerATK);
or
decreaseHitPointsBy (playerATK); // this approach would require a corresponding
// increaseHitPointsBy (value) method
This question already has answers here:
Error "main class not found"
(3 answers)
Closed 8 years ago.
For some reason that I haven't been able to figure out, the program I've been working on for a while now isn't running. This is the error message I've been getting:
All I did between the time it was working and now was add another class, but even after deleting it the error message is still here. Netbeans has also been unhelpful, there aren't any errors marked in red in the code. I'm still a beginner to Java, so I don't know how much is needed to find the error, so here is all of the code:
public class ChooseYourOwn {
static class Character {
private static int str;
public Character() {
int str = 0;
int spd = 0;
int agl = 0;
int def = 0;
int mag = 0;
int wil = 0;
int mp = 0;
int hp = 0;
}
private Character(String name) {
}
}
/**
*
* #param args
* #return
*/
public static int main(String[] args) {
//set up character
System.out.println("Choose a class:");
String [] genusOptions;
genusOptions = new String[] {"\n1-Warrior\n2-Mage\n3-Thief"};
System.out.println(Arrays.toString(genusOptions));
Scanner input1 = new Scanner(System.in);
String genus;
genus = input1.nextLine();
System.out.println("Your character's stats are:");
switch (genus) {
case "1":
{
String[] array = new String[] {"\nStrength-15\nSpeed-5\nAgility-5\nDefense-10\nMagicDamage-5\nMagicDefense-0\nMagicPoints-5\nHitPoints-15\n"};
System.out.println(Arrays.toString(array));
int str = 5;
int spd = 2;
int agl = 2;
int def = 5;
int mag = 0;
int wil = 0;
int mp = 0;
int hp = 10;
break;
}
case "2":
{
String[] array = new String[] {"\nStrength-5\nSpeed-5\nAgility-5\nDefense-0\nMagicDamage-15\nMagicDefense-10\nMagicPoints-15\nHitPoints-5\n"};
System.out.println(Arrays.toString(array));
int str = 5;
int spd = 5;
int agl = 5;
int def = 0;
int mag = 10;
int wil = 10;
int mp = 15;
int hp = 5;
break;
}
case "3":
{
String[] array = new String[] {"\nStrength-10\nSpeed-15\nAgility-15\nDefense-10\nMagicDamage-10\nMagicDefense-5\nMagicPoints-10\nHitPoints-10\n"};
System.out.println(Arrays.toString(array));
int str = 10;
int spd = 15;
int agl = 15;
int def = 10;
int mag = 10;
int wil = 5;
int mp = 10;
int hp = 10;
break;
}
}
String[] array = new String[] {"\n1-Move\n2-Rest\n3-Attack\n4-Run\n5-Activate lever/button/chest\n6-Inventory\n7-Skills"};
System.out.println("Choose a number to continue");
System.out.println(Arrays.toString(array));
Scanner input2;
input2 = new Scanner(System.in);
String action;
action = input2.nextLine();
switch (action) {
case "1":
{
System.out.println("");
//not done yet
break;
}
case "2":
{
System.out.println("");
//Rest, restores some health
break;
}
case "3":
{
System.out.println("");
int max = 100;
int min = 0;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
int damage = 0;
if (10 > randomNum) {
damage = (Character.str/100) * 10;
} else if (20 > randomNum) {
damage = (Character.str/100) * 20;
} else if (30 > randomNum) {
damage = (Character.str/100) * 30;
} else if (40 > randomNum) {
damage = (Character.str/100) * 40;
} else if (50 > randomNum) {
damage = (Character.str/100) * 50;
} else if (60 > randomNum) {
damage = (Character.str/100) * 60;
} else if (70 > randomNum) {
damage = (Character.str/100) * 70;
} else if (80 > randomNum) {
damage = (Character.str/100) * 80;
} else if (90 > randomNum) {
damage = (Character.str/100) * 90;
} else if (100 > randomNum) {
damage = Character.str;
}
System.out.println( damage );
break;
}
case "4":
{
System.out.println("");
//not done yet
break;
}
case "5":
{
System.out.println("");
//not done yet
break;
}
case "6":
{
System.out.println("");
//not done yet
break;
}
case "7":
{
System.out.println("");
//not done yet
}
}
return (0);
}
}
There have been similar questions asked here before, but none of them helped and others were very unclear. A good and coherent answer would be much appreciated, thanks.
In Java, the main method signature is public static void main(String[] args), however in your code you are using public static int main(String[] args).
Java main methods are declared like this:
public static void main(String[] args) {
...
}
More information can be found at Can a main method in Java return something?
You can't change the return type of main. This,
public static int main(String[] args)
must be one of (per this worthwhile Wikipedia article on Entry point)
public static void main(String[] args)
public static void main(String... args)
public static void main(String args[])
All of which are void, if you need to set a UNIX style return type, you can use System.exit(int); to quote the Javadoc that
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.