I am currently working on an assignment that requires me to create a "bottle class" for a pre-made "bottle demo" file my professor made. This is the description of the assignment:
Write a Bottle class. The class has these 14 methods: read(), set(int), >set(Bottle), get(), and(Bottle), subtract(Bottle), multiply(Bottle), >divide(Bottle), add(int), subtract(int), multiply(int), divide(int), >equals(Bottle), and toString(). The toString() method will be given in class. All >add, subtract, multiply, and divide methods return a Bottle. Your Bottle class >must guarantee bottles always have a positive value and never exceed a maximum >number chosen by you. These numbers are declared as constants of the class. Each >method wit ha parameter must be examined to determine if the upper or lower bound >could be violated. Consider each method carefully and test only the conditions >that could be violated.
And here is the demo code:
public static void main(String[] args) {
int x;
bottle bottle1 = new bottle();
bottle bottle2 = new bottle();
bottle bottle3 = new bottle();
bottle bottle4 = new bottle();
bottle bottle5 = new bottle();
System.out.println("please enter a number for bottle1:");
bottle1.read(); // affected my max and min
System.out.println("Bottle1 is this value " + bottle1 + ".");
System.out.println("Please enter a number for bottle2:");
bottle2.read(); // affected by max and min
bottle3.set(0);
bottle3 = bottle3.add(bottle1);
bottle3 = bottle3.add(bottle2);
bottle3 = bottle3.divide(2);
System.out.println("The 2 bottle average is: " + bottle3 + ".");
System.out.print("Subtracting bottle1 from bottle2 is: " );
bottle3 = bottle2.subtract(bottle1);
System.out.println( bottle3);
bottle3 = bottle2.divide(bottle1);
System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
if (bottle1.equals(bottle2))
{
System.out.println("Bottle1 and bottle2 are equal.");
}
else
{
System.out.println("Bottle1 and bottle2 are not equal.");
}
System.out.println("Bottle4 is now given the value of 10 with the set() method.");
bottle4.set(10);
System.out.println("The value of bottle4 is " + bottle4 + ".");
System.out.println("Bottle4 is now multiplied with bottle1. The value is placed in bottle5.");
bottle5 = bottle1.multiply(bottle4);
System.out.println("The value of bottle5 is " + bottle5 + ".");
System.out.println("Enter an integer to add to the value bottle1 has.");
System.out.println("The sum will be put in bottle3.");
Scanner keyboard = new Scanner(System.in);
x = keyboard.nextInt();
bottle3 = bottle1.add(x);
System.out.println("Adding your number " + x +
" to bottle1 gives a new Bottle with " + bottle3 + " in it.");
System.out.print("Adding the number " + bottle2 + " which is the number" +
" in bottle2 to the\nnumber in ");
bottle2 = bottle1.add(bottle2);
System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + ".");
}
}
And this is the code I have made so far:
public class bottle {
Scanner scan = new Scanner(System.in);
private int value;
public void Bottle() {
value = 0;
}
public void read() {
value = scan.nextInt();
}
public void set(bottle) {
value = bottle1.value;
}
public void set(int bottle1) {
value = bottle1;
}
public bottle add(bottle) {
value = value + bottle1.value;
}
public bottle subtract(bottle) {
}
public bottle multiply(bottle) {
}
public bottle divide(bottle) {
}
public bottle add(int bottle) {
}
public bottle subtract(int bottle) {
}
public bottle multiply(int bottle) {
}
public bottle divide(int bottle) {
value = value / bottle;
}
public String toString() {
String name = null;
return name;
}
public boolean equals(bottle bottle) {
if (this == bottle) {
return true;
}
else {
return false;
}
}
}
What I need help on is how do I get my methods to work? ( add(int), divide(bottle), divide(int), etc)
And for there to be a max and min for values the user can input, I know that it can be placed at the top of the class code, but how do I make it so that every time the user inputs a number and the math outputs that the max and min will be checked every time to see if any number violates the set rule?
My I know my class code is missing many key components (I think return methods for the math parts) but I am struggling to stay sane trying to figure out what to do. Any help would be greatly appreciated and thanks in advance.
I will also answer any questions that you might have to the best of my ability.
EDIT: I have remade my code after reading the chapter about classes in my textbook and my knowledge is a bit better than before. Here is my new code:
Scanner scan = new Scanner(System.in);
private int value;
private int max = 100;
private int min = 0;
public bottle() {
// sets default value as zero
this.value = 0;
}
public void read() {
value = scan.nextInt();
}
public void set(int value) {
this.value = value;
}
public int add(bottle) {
if (this.value + bottle < this.max && this.value + bottle > this.min)
return this.value + bottle;
else
System.out.println("Please enter another number");
int x = scan.nextInt();
return add(x);
// the few lines above checks to see if the number violates the max and min
}
public int subtract(bottle) {
if (this.value - bottle < this.max && this.value - bottle > this.min)
return this.value - bottle;
else
System.out.println("Please enter another number");
int x = scan.nextInt();
return subtract(x);
}
// though there is this error under the word bottle in the parentheses
public int multiply(bottle) {
if (this.value * bottle < this.max && this.value * bottle > this.min)
return this.value * bottle;
else
System.out.println("Please enter another number");
int x = scan.nextInt();
return multiply(x);
}
public int divide(bottle) {
if (this.value / bottle < this.max && this.value / bottle > this.min)
return this.value / bottle;
else
System.out.println("Please enter another number");
int x = scan.nextInt();
return divide(x);
}
// the String toString method, format as shown by the professor.
public String toString()
{
return this.max + " " + this.min + " " + this.value;
Though I still have 4 errors in my class which is the word bottle inside the parentheses after my add, subtract, multiply, and divide method. Thus the demo file has 8 errors which are all the math methods. I am not sure what to do because "bottle" is an object right? Then how do I add 2 bottles together, or am I taking the wrong approach?
It looks like you're almost on the right track. These will help:
https://www.tutorialspoint.com/java/java_object_classes.htm
https://www.tutorialspoint.com/java/java_methods.htm
Pay attention to what an instance is and also how passing in arguments to methods works along with returning.
Related
This is probably a dumb question. My following code looks fine but on the output, it does not expect results from my testing scenarios. Code follows:
import java.util.Scanner;
public class PartyPlannerLab {
public static Scanner input = new Scanner(System.in);
public static int getGuestCount(int guests) {
while(true) {
System.out.print("Enter number of guests: ");
guests = input.nextInt();
if (guests >= 1 && guests <= 100)
break;
else
System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
}
return guests;
}
public static int getSlicesPerPerson(int slicesPerPerson) {
while(true) {
System.out.print("Enter number of slices per person: ");
slicesPerPerson = input.nextInt();
if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
break;
else
System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
}
return slicesPerPerson;
}
public static double computeRoomCost(int guests, double roomCost) {
if (guests <= 30)
roomCost = 100.00;
else
roomCost = 200.00;
return roomCost;
}
public static double computeSodaCost(double sodaCost, int guests) {
sodaCost = guests * 1.50;
return sodaCost;
}
public static void printSummary(int guests, double roomCost, double sodaCost, double pizzaCost) {
System.out.println("Total Guests: " + guests);
System.out.println("RoomCost: $" + roomCost);
System.out.println("SodaCost: $" + sodaCost);
System.out.println("PizzaCost: $" + pizzaCost);
System.out.println("Total Cost: $" +(roomCost + sodaCost + pizzaCost));
}
public static void main(String[] args) {
int guests = 0;
int slicesPerPerson = 0;
double roomCost = 0.0;
double sodaCost = 0.0;
double pizzaCost = 0.0;
getGuestCount(guests);
getSlicesPerPerson(slicesPerPerson);
computeRoomCost(guests, roomCost);
computeSodaCost(sodaCost, guests);
printSummary(guests, roomCost, sodaCost, pizzaCost);
input.close();
}
}
One output is as follows:
Enter number of guests: 10
Enter number of slices per person: 2
Total Guests: 0
RoomCost: $0.0
SodaCost: $0.0
PizzaCost: $0.0
Total Cost: $0.0
You are not making use of the return values of getGuestCount, getSlicesPerPerson etc.
Those methods return a value, which basically means that you can use them as if they are a value. input.nextInt returns a value too, which is why you can put it on the right of =.
Inside the method, getGuestCount seems to change the value of guests passed in, but this change won't actually reflect on the caller's side, because Java is pass-by-value. You are kind of throwing away the value that was passed in.
In fact, your methods will only work as they are if the arguments are passed by reference, so that the methods can modify the variables passed in. But this is not possible in Java. See this post for the difference between pass-by-value and pass-by-reference.
The right way to rewrite your methods in Java is to return the value (which they are already doing, but you are not making use of the return value), and remove the extraneous parameter.
public static int getGuestCount() {
int guests;
while(true) {
System.out.print("Enter number of guests: ");
guests = input.nextInt();
if (guests >= 1 && guests <= 100)
break;
else
System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
}
return guests;
}
public static int getSlicesPerPerson() {
int slicesPerPerson;
while(true) {
System.out.print("Enter number of slices per person: ");
slicesPerPerson = input.nextInt();
if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
break;
else
System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
}
return slicesPerPerson;
}
public static double computeRoomCost(int guests) {
double roomCost;
if (guests <= 30)
roomCost = 100.00;
else
roomCost = 200.00;
return roomCost;
}
public static double computeSodaCost(int guests) {
double sodaCost = guests * 1.50;
return sodaCost;
}
This is how you "make use of the return values": instead of passing in the variable you want the method to modify, put it on the left hand side of = in an assignment statement:
guests = getGuestCount();
slicesPerPerson = getSlicesPerPerson();
roomCost = computeRoomCost(guests);
sodaCost = computeSodaCost(guests);
The reason why you are not getting the output is that the values that you initialized in your main method are not being updated with the method calls you are making.
Below code might solve the problem you are facing -
public static void main(String[] args) {
int guests = 0;
int slicesPerPerson = 0;
double roomCost = 0.0;
double sodaCost = 0.0;
double pizzaCost = 0.0;
guests = getGuestCount(guests);
slicesPerPerson = getSlicesPerPerson(slicesPerPerson);
roomCost = computeRoomCost(guests, roomCost);
sodaCost = computeSodaCost(sodaCost, guests);
printSummary(guests, roomCost, sodaCost, pizzaCost);
input.close();
}
Note- There is no need of passing the parameter in the methods getGuestCount & getSlicesPerPerson as the input is being taken from I/O
I'm writing a dice game program that creates a die for the user and computer, then asks for user name, number of sides on dice, and how many iterations the user would like to play with the computer. Then have the program check after each roll to see which roll is higher and at the end of the iterations it writes back the total wins for each user and computer and who the winner is. The problem i'm having is that I need to create a roll() class to randomize the dice and set it to a value for the users roll and computers roll and i'm getting this error...
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at classwork6_3.Die.roll(Die.java:52)
at classwork6_3.ClassWork6_3.main(ClassWork6_3.java:29)
Here is my main() class...
package classwork6_3;
import java.util.*;
public class ClassWork6_3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Random r = new Random();
System.out.print("Enter user's name: ");
String name = s.nextLine();
int value = 0;
int value2 = 0;
System.out.print("How many sides does the die have?: ");
int sides = s.nextInt();
s.nextLine();
System.out.print("How many times would you like to roll?: ");
int numRoll = s.nextInt();
int counter = 0;
int counter2 = 0;
Die user = new Die(name, sides, value);
Die comp = new Die(value);
for(int i = 1; i<= numRoll; i++){
value = r.nextInt(user.roll(sides)) + 1;
value2 = r.nextInt(comp.roll(sides)) + 1;
System.out.printf("%s rolled: %d\n", user.getOwner(), user.getValue());
System.out.print("Computer rolled: " + comp.getValue() + "\n\n");
if(value > value2){
counter++;
} else if(value2 > value){
counter2++;
}
}
System.out.printf("%s TOTAL WINS: %d\n", user.getOwner(), counter);
System.out.print("Computer TOTAL WINS: " + counter2 + "\n\n");
if(counter > counter2){
System.out.printf("%s wins!!\n", user.getOwner());
}else if(counter2 > counter){
System.out.print("Computer wins\n");
}else{
System.out.print("It's a tie!\n");
}
}
}
Here is my Die() class...
package classwork6_3;
import java.util.*;
public class Die {
Random r = new Random();
private int sides;
private int value;
private String owner;
public Die(String owner, int sides, int value){
this.sides = sides;
this.value = value;
this.owner = owner;
}
public Die (int value){
this.value = value;
}
public int getSides(){
return sides;
}
public int getValue(){
return value;
}
public String getOwner(){
return owner;
}
public void setSides(int sides){
this.sides = sides;
}
public void setValue(int value){
this.value = value;
}
public void setOwner(String owner){
this.owner = owner;
}
public int roll(int rand){
rand = r.nextInt(value);
return value;
}
}
Here are the issues with your code :
You are never returning the random value generated by the roll(...) method
public int roll(int rand){
rand = r.nextInt(value);
return value;
}
Change it to
public int roll(int rand) {
return r.nextInt(rand);
}
Within the for-loop all you need to do is just call the roll()
method. Since it already calculates the random value of the die, there is no need for you to call r.nextInt() again.
value = r.nextInt(user.roll(sides)) + 1;
value2 = r.nextInt(comp.roll(sides)) + 1;
Change it to
value = user.roll(sides) + 1;
value2 = comp.roll(sides) + 1;
Now print out the values using :
System.out.printf("%s rolled: %d\n", user.getOwner(), value);
System.out.print("Computer rolled: " + value2 + "\n\n");
Your code will work as expected after performing the above mentioned steps. Also as a side note do not use variable names that have an ambiguous meaning like value, value2 etc.
Whenever you make a new die, your value is never changed from zero, causing that error to pop-up since the range it would be looking through whenever you call roll(), acording to your Die class, is 0 to 0. Change value to the value the user puts in and the make a new die.
I wanna store at most 10 numbers but after I ran the programe, I couldn't print the name and the sentence of "The system is full!!" when the numbers exceed 10. How can I correct it?
public static void saveContact(String name, int number) {
nameRec[nameCounter]= name;
nameCounter++;
boolean full = false;
int i = 0;
while(i<=9) {
full = true;
break;
}
i++;
if(!full) {
System.out.println("The System is full!!!");
}else {
System.out.println("Enter the phone number:");
}
System.out.println("Saving the number of\n" + name+ ":" + number);
}
public static String[] nameRec = new String[10];
private static int nameCounter;
public static void saveContact(String name, int number) {
if (nameCounter >= 10) {
System.out.println("The System is full!!!");
} else {
System.out.println("Saving the number of\n" + name + ":" + number);
nameRec[nameCounter++] = name;
}
}
You can build condition just from nameCounter to satisfy your goal.
Variable i is never getting incremented inside the while loop. Also where have namerec & nameCounter been declared. They might be class attributes. If not you have to declare them in a suitable place.
When the breed method is called and there is food available, the size of the colony doubles. The user is asked how many times they want to feed the colony and how many times they want the colony to breed. I need to output the amount of times that it successfully bred. I created an integer called success to keep track of the times it successfully bred, but (not surprisingly for me) does not work and returns 0. How can I fix this problem? Thanks.
AmoebaColony tester class
import javax.swing.JOptionPane;
public class AmoebaColonyTester {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null, "What will be the name of the colony?");
String caretakerName = JOptionPane.showInputDialog(null, "What is the name of the caretaker of the colony?");
int colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the starting size of the colony?"));
int feedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want to feed the colony?"));
int breedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want your colony to breed?"));
int vitamin = JOptionPane.showConfirmDialog(null,"Do you want to give vitamins to your colony?", "Please select",
JOptionPane.YES_NO_OPTION);
boolean isVitamin;
if (vitamin == 1)
isVitamin = true;
else
isVitamin = false;
int success = 0;
AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, feedTimes,
breedTimes, isVitamin);
for(int x = 1; x <= breedTimes; x++)
{
success += amoeba.breed();
}
amoeba.howManyDead();
System.out.println("the size of the colony is " + amoeba.getSize());
JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
"\nThe starting colony size was " + colonySize + "\nThey were fed " + feedTimes
+ " times \nRequested number of times to breed: " + breedTimes
+ "\nThey successfully bred " + success + "times \nThe final size of the"
+ " colony is " + amoeba.getSize());
}
}
AmoebaColony
public class AmoebaColony {
private String name;
private String caretakerName;
private int colonySize;
private int feedTimes;
private int breedTimes;
boolean isVitamin;
int successfulBreeds;
public AmoebaColony(String name, String caretakerName, int colonySize,
int feedTimes, int breedTimes, boolean isVitamin) {
this.name = name;
this.caretakerName = caretakerName;
this.colonySize = colonySize;
this.feedTimes = feedTimes;
this.breedTimes = breedTimes;
this.isVitamin = isVitamin;
}
public int getSize()
{
return colonySize;
}
public int breed()
{
if(breedTimes <= feedTimes){
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
}
else{
feedTimes--;
breedTimes--;
return 0;
}
}
public int howManyDead()
{
Random random = new Random();
int pop = colonySize;
if (isVitamin)
{
int c1 = random.nextInt(4);
if (c1 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
else if (isVitamin == false)
{
int c2 = 1 + random.nextInt(3);
if (c2 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
return pop - colonySize;
}
}
Sample input:
Colony name : bobo
Caretaker name : Peter
colony size : 500
feed times: 4
breed times: 7 // in this case it will breed successfully 4 times
isVitamin: No vitamins (false)
I would recommend just running this through some simple debugging. Follow these steps and it should narrow down where your problem is.
Print the returned value of .breed(). If its anything other then what you expect, your function is not returning properly.
Check that the values of your breed() function are set properly. E.g. they are actually the values you are inputting at each step. I would add print statements to see if this is the case.
Compare this to the output values. If they don't match (e.g. your success was right when it was calculated, but not when you print it at the end), then look for something that might be making the change in the middle of the process.
From what I can see of the code, I do not see a reason it should not properly update the success.
Edit I see your problem. You are inputting the values specified, and this is how the program interprets it. Your breed function uses the following values, you enter breed=7, feed=4. I put these values into the function below...
if(7<= 4){ // this is obviously false, so it uses the else
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
} else{
//Since this is run, and both are decremented togeather, you will always be in the Else.
feedTimes--;
breedTimes--;
return 0;
}
I feel like this has an extremely obvious answer, so forgive me if I'm being stupid.
I'm creating a simple text-based game for elementary school kids in which the user, playing as a mage, needs to ward off mighty dragons by mixing together various spell cards (in my case, multiplying their values together to try and get the closest to the dragon's weakness number). This weakness number changes every turn, until the dragon hits an HP less than or equal too 0, at which point another dragon will spawn and the player will have to ward that one off too. The entire project takes up five classes, one for the player, another for the cards, another for the dragon, another for the game's play methods, and a fifth, the driver.
The first thing that comes to mind for me is to make a method in Player.java that goes something like this:
public int isClosestToo(int num, int max){
int counter = 0;
for(int i = num; i <= max; i++){
counter++
}
return counter;
}
followed by an if statement in my game class' play method:
if(isClosestToo(num1) > isClosestToo(num2){
/*do something*/
}
I realize that this would work, but I want to make sure I'm not missing something blatantly obvious and simpler that I could do. Some method in the Integer class maybe?
Please keep in mind this is a very early version of the project, and I hope to implement Slick2D graphics later.
Here's all of my code thus far:
From Game.java:
import java.util.Scanner;
public class Game {
Scanner scan = new Scanner(System.in);
public Game(){}
public void play(){
Player p = new Player();
while(p.getHP() > 0){
Dragon d = new Dragon();
System.out.println("You have " + p.getHP() + " HP");
System.out.println(d);
System.out.println(p.elementHandToString());
System.out.println(p.attackHandToString());
System.out.println("Choose the two cards that multiply to be closest too the weakness of the dragon for the most damage!");
int element = scan.nextInt();
int attack = scan.nextInt();
int damage = d.getWeakness() - (p.getElementCard(element - 1).getNum() * p.getAttackCard(attack - 1).getNum());
d.setHP(d.getHP() - damage);
p.afterTurn(element, attack);
d.newWeakness();
}
}
}
And Player.java:
import java.util.Random;
import java.util.ArrayList;
public class Player {
Random r = new Random();
int hp;
ArrayList<Card> attackHand;
ArrayList<Card> elementHand;
ArrayList<String> elements;
ArrayList<String> attacks;
public Player(){
this.hp = 100;
this.genHands();
attackHand = new ArrayList<Card>();
elementHand = new ArrayList<Card>();
//arraylist of types of elements, feel free to add more using the .add method
elements = new ArrayList<String>();
elements.add("Fire");
elements.add("Ice");
elements.add("Water");
elements.add("Air");
elements.add("Rock");
elements.add("Mana");
//arraylist of types of attacks, feel free to add more using the .add method
attacks = new ArrayList<String>();
attacks.add("Projectile");
attacks.add("Self");
attacks.add("Explosion");
attacks.add("Repeated");
attacks.add("Debuff");
attacks.add("Melee");
}
public Player(int hp){ this.hp = hp; }
public int getHP(){ return hp; }
public ArrayList<Card> getAttackHand(){ return attackHand; }
public ArrayList<Card> getElementHand(){ return elementHand; }
public Card getAttackCard(int whichCard){ return attackHand.get(whichCard); }
public Card getElementCard(int whichCard){ return elementHand.get(whichCard); }
public void afterTurn(int element, int attack){
attackHand.add(new Card(genRand(1, 12), attacks.get(genRand(1, 12))));
elementHand.add(new Card(genRand(1, 12), elements.get(genRand(1,12))));
attackHand.remove(attack - 1);
elementHand.remove(element - 1);
}
public String elementHandToString(){ return "Card 1: " + elementHand.get(0) + "/nCard 2: " + elementHand.get(1) + "/nCard 3: " + elementHand.get(2) + "/nCard 4: " + elementHand.get(3); }
public String attackHandToString(){ return "Card 1: " + attackHand.get(0) + "/nCard 2: " + attackHand.get(1) + "/nCard 3: " + attackHand.get(2) + "/nCard 4: " + attackHand.get(3); }
//generates the player's hands
public void genHands(){
//creates a deck of random elemental cards
for(int x = 0; x <= 4; x++){
elementHand.add(new Card(genRand(1, 12), elements.get(genRand(0, elements.size() - 1))));
}
//creates a deck of random attack cards
for(int i = 0; i <= 4; i++){
attackHand.add(new Card(genRand(1, 12), attacks.get(genRand(0, attacks.size() - 1))));
}
}
//returns a random integer between min and max
//#param1 minimum number for random to be
//#param2 maximum number for random to be
public int genRand(int min, int max){ return r.nextInt(max) + min; }
}
Dragon.java:
package alchemy;
import java.util.Random;
public class Dragon {
int hp;
int weakness;
Random r = new Random();
public Dragon(){
hp = genRand(1, 144);
weakness = genRand(1, hp);
}
public int getHP(){
return hp;
}
public void setHP(int newHP){
hp = newHP;
}
public String toString(){
return "A dragon with " + hp + "HP has appeared!" + "/nIts current weakness is " + weakness + "/nPlay two cards that multiply to a number close to its weakness for damage!";
}
public int getWeakness(){
return weakness;
}
public void newWeakness(){
weakness = genRand(1, hp);
}
public int genRand(int min, int max){
return r.nextInt(max) + min;
}
}
And lastly, Card.java:
import java.util.Random;
public class Card{
int num;
Random r = new Random();
String element;
public Card(){
num = 0;
element = "n/a";
}
public Card(int n, String e){
num = n;
element = e;
}
public int getNum(){
return num;
}
public String getElement(){
return element;
}
public String toString(){
return "This is a " + element + "card with a value of " + num + ".";
}
}
Primarily, the issue I'm running into is the play() loop of the Game class, where I need to calculate the damage that the player does. Obviously, (before I program in elemental logic), I want that damage to actually do damage instead of ending up as a negative number and making the dragon gain HP. Any and all help would be greatly appreciated. Thank you!
-Nick
In java, when a method starts with "is", it returns boolean. But your project probably maintained by you yourself, it can be whatever you want.
Your isClosestToo method, takes in 2 arguments, which is num and max, both integer, but when you call it, you only takes in one argument.
public int isClosestToo(int num, int max){
int counter = 0;
for(int i = num; i <= max; i++){
counter++
}
return counter;
}
if(isClosestToo(num1) > isClosestToo(num2){
/*do something*/
}
the method name itself is confusing, you can do public int ClosestTo(int[] num,int max) to take and a list of number and return the closest number or do DiffMax(int num) to take in a number and subtract it with the max, in which this case your max must be accessible in the method scope.
public int ClosestTo(int[] num,int max)
{
1.declare a variable to store the closest num
2.for every element in num, if Math.abs(max-num) < closestNum, store num to closestNum
3.return closestNum
}
or
public int DiffMax(int num)
{
1. return Math.abs(max-num) < closestNum
}
then you can do
if(DiffMax(num1) > DiffMax(num2){
/*do something*/
}
this is an option
if((max-int1)>(max-int2) && max-int2>=0){
//do something with int2
}
else if((max-int1)<(max-int2) && max-int1>=0){
//do something with int1
}
else if(max-int1>=0){
//do something with int1
}
else if(max-int2>=0){
//do something with int2
}
else{
//do something if conditions aren't met
}