This is my code, my questions is with my deal method, how do I get it to inclement the to a different number every time I call it and also how to create a Boolean method. This is my code, my questions is with my deal method, how do I get it to inclement the to a different number every time I call it and also how to create a Boolean method.
package Card;
import java.util.Random;
/**
*
* #author Mr. Pierre
*/
public class Card {
private int SuitRank;
private int CardRank;
private String cardValue;
//My constructor
public Card()
{
SuitRank=1;
CardRank=2;
}
//My deal method
void dealCard()
{
SuitRank++;
Random randomGenerator = new Random();
int SuitRank = randomGenerator.nextInt(4)+1;
CardRank++;
Random randomGenerator1 = new Random();
int CardRank= randomGenerator1.nextInt(13)+2;
}
//My compare method
public int compare(Card otherCard)
{
if (otherCard.getCardRank() > CardRank)
return 1;
if (otherCard.getCardRank() == CardRank)
{
if (otherCard.getSuitRank() > SuitRank)
return 1;
if (otherCard.getSuitRank()< SuitRank)
return -1;
if (otherCard.getSuitRank()==SuitRank)
return 0;
}
if (otherCard.getCardRank() < CardRank)
return -1;
return CardRank;
}
//my Get suitrank method
public int getSuitRank()
{
SuitRank++;
return SuitRank;
}
public String getSuitName ()
{
String SuitName="";
if( SuitRank == 1){
SuitName = "Clubs";
}
else if(SuitRank == 2){
SuitName = "Diamonds";
}
else if(SuitRank == 3){
SuitName = "Hearts";
}
else if(SuitRank == 4){
SuitName = "Spades";
}
return SuitName;
}
public int getCardRank ()
{
return CardRank;
}
public String getCardName ()
{
String CardName="";
if(CardRank==2){
CardName="Duce";
}
else if(CardRank==3){
CardName="Three";
}
else if(CardRank==3){
CardName="Three";
}
else if(CardRank==4){
CardName="Four";
}
else if(CardRank==5){
CardName="Five";
}
else if(CardRank==6){
CardName="Six";
}
else if(CardRank==7){
CardName="Seven";
}
else if(CardRank==8){
CardName="Eight";
}
else if(CardRank==9){
CardName="Nine";
}
else if(CardRank==10){
CardName="Ten";
}
else if(CardRank==11){
CardName="Jack";
}
else if(CardRank==12){
CardName="Queen";
}
else if(CardRank==13){
CardName="King";
}
else if(CardRank==14){
CardName="Ace";
}
return CardName;
}
public String toString()
{
return getCardName()+ " of " +getSuitName();
}
}
int SuitRank = randomGenerator.nextInt(4)+1;
The int means you're creating a local variable instead of modyfying a class member. Also, why are you calling SuitRank++ if you're planning to set SuitRank to a random value right away? Same applies to CardRank.
As for the Boolean method - it's just public Boolean method(...).
Related
My goal is to have 2 different objects fight each other, and show the results. My problem is I cant figure out how to set the attack and health properly so that it actually updates the way it is supposted to.
import java.util.Random;
import java.util.Scanner;
/**
*
* #author Brenton
*/
public class Fighter {
private String name;
private int attack;
private int level = 1;
private int health = 50;
private boolean isAlive = true;
private Fighter fighterTwo;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAttack() {
Random generator = new Random();
attack = generator.nextInt(10) * level + 1;
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getLevel() {
if(level >= 60)
{
level = 60;
}
return this.level;
}
public void setLevel(int level) {
this.level = level;
}
public int getHealth() {
if(this.health <= 0)
{
this.health = 0;
}
return this.health;
}
public void setHealth(int health) {
this.health = health;
}
public boolean isAlive() {
if(this.health <= 0)
{
this.isAlive = false;
}
return this.isAlive;
}
public static String getWelcome() {
String welcome = "Hello and welcome to FightClub, do you wish to fight, yes or no? ";
return welcome;
}
public String getPunch(Fighter fighterTwo) {
this.fighterTwo = fighterTwo;
String hit = "You choose to punch the other fighter and dealt " + getAttack() + " damage, your opponent now has " + this.decreaseHitPoints(fighterTwo) + " health remaining";
return hit;
}
public int decreaseHitPoints(Fighter fighterTwo) {
this.fighterTwo = fighterTwo;
int health = fighterTwo.getHealth();
int attack = getAttack();
health = health - attack;
return health;
}
public static String invalidInput() {
String invalid = "I am sorry that is not a valid input option ";
return invalid;
}
public void getWinner(Fighter fighterTwo) {
this.fighterTwo = fighterTwo;
if(this.isAlive() == false && fighterTwo.isAlive() == false)
{
System.out.println("Both fighters have fallen heroically");
}
else if(this.isAlive() == true && fighterTwo.isAlive() == false)
{
System.out.println(this.getName() + " is victorious! ");
}
else if(this.isAlive() == false && fighterTwo.isAlive() == true)
{
System.out.println(fighterTwo + " is victorious! ");
}
else
{
System.out.println("ERROR ERROR ERROR");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Fighter a = new Warrior();
Fighter b = new Dragon();
System.out.print(getWelcome());
while(in.hasNextLine())
{
switch(in.nextLine())
{
case "no":
System.out.println("Wow, you are not even gonna try, you have lost!");
break;
case "yes":
System.out.println("Let the fight begin! ");
while(a.isAlive() && b.isAlive())
{
System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
switch(in.nextLine())
{
case "punch":
System.out.println(a.getPunch(b));
break;
/*case "kick":
System.out.println(a.getKick(b));
break;
case "headbutt":
System.out.println(a.getHeadbutt(b));
break;*/
default :
System.out.println(invalidInput());
break;
}
}
default:
System.out.println(invalidInput());
break;
}//end of first switch statement
}//end of first while loop
}//end of main
}
You're calculating the attack correctly. You're just not updating the state of the other fighter.
In your main() method you launch the attack with
System.out.println(a.getPunch(b));
That's just fine. a throws a Punch at b, then you print out the hit points returned from getPunch(). So let's dig deeper into getPunch() to try to find the problem.
In getPunch() you end up invoking
this.decreaseHitPoints(fighterTwo)
while constructing the return String. This seems like the right approach, so is there a problem in decreaseHitPoints()?
public int decreaseHitPoints(Fighter fighterTwo) {
this.fighterTwo = fighterTwo;
int health = fighterTwo.getHealth();
int attack = getAttack();
health = health - attack;
return health;
}
You assign the fighterTwo argument to your fighterTwo field. Not sure why, but that's not wrong per se. Then you get his health into a local variable called health. Then you get the attack into a local variable called attack. Then you subtract attack from health, and then return the calculated value. But you never update the health value on fighterTwo! So you just need one more line in your program: right before your return statement, insert
fighterTwo.setHealth(health);
I am new to Java. Stack with passing the sum value of PairOfDice class into DiceRollerapp Class, where, depending on the sum, a different message displays. No matter what, sum comes in as 0 in DiceRollerapp. Any help is appreciated greatly.
public class PairOfDice extends Die {
private int sum;
private int d1,d2;
public PairOfDice() {
super();
}
public PairOfDice(int sum){
this.sum=sum;
}
public int getValue1() {
d1=super.getValue();
return d1;
}
public int getValue2() {
d2=super.getValue();
return d2;
}
public int getSum(){
sum=d1+d2;
return sum;
}
public void setSum(int sum){
this.sum=sum;
}
}
And:
public class DiceRollerapp extends PairOfDice{
private int total;
public DiceRollerapp() {
super();
}
public DiceRollerapp(int sum) {
super(sum);
}
public String getMessage() {
total=super.getSum();
if (total == 7) {
System.out.println("CRAPS!");
} else if (total == 12) {
System.out.println("BOX CARS!");
} else if (total == 2) {
System.out.println("SNAKE EYES!");
} else {
System.out.println("");
}
return "";
}
}
The issue is that
public int getSum(){
sum=d1+d2;
return sum;
}
Always recalculates the sum based on d1 and d2. That means you must set them before you call getSum().
public String getMessage() {
super.getValue1(); // <-- sets d1.
super.getValue2(); // <-- sets d2
total=super.getSum(); // <-- adds d1 and d2.
if (total == 7) {
System.out.println("CRAPS!");
} else if (total == 12) {
System.out.println("BOX CARS!");
} else if (total == 2) {
System.out.println("SNAKE EYES!");
} else {
System.out.println("");
}
return "";
}
i have a question. i have to create 3 methods testUreaRisk, testProteinRisk and printResults. and the below is 2 out of the 3.
public class Lab {
public static String testUreaRisk(double ureaLevel)
{
if ((ureaLevel < 0) || (ureaLevel > 10))
return "0";
else if (ureaLevel <= 4.0)
return "-1";
else
return "1";
}
public static String testProteinRisk(double proLevel)
{
if ((proLevel < 0) || (proLevel >150))
return "0";
else if (proLevel >= 67.0)
return "1";
else
return "-1";
}
so my problem is can i put a value into the return number 1, 0,-1 as -1 = low risk, 0 = cannot be defined and 1 = high risk? if can,how? because the 3rd method could only allow me to return a string that show the result (low risk,cannot be defined and high risk) instead of the number(-1,0,1).thanks
But better way is use an Enum.
Eg:
public enum Enum {
LOW("-1"), NOT_DETERMINED("0"), HIGH("1");
}
Eg:
My Enum class
public enum Enum {
LOW("-1"), NOT_DETERMINED("0"), HIGH("1");
private String code;
private Enum(String c) {
this.code = c;
}
public String getCode() {
return this.code;
}
public static Enum getEnum(String code) {
switch (code) {
case "-1":
return LOW;
case "0":
return NOT_DETERMINED;
case "1":
return HIGH;
default:
return null;
}
}
}
Now
System.out.println(Enum.getEnum(testProteinRisk(10)));
Will give you
LOW
You should go with an Enum class here. If you want to have int values, you could create in each enum a emthod that will re`enter code hereturn this value:)
Enum { LOW(-1), NOT_DETERMINED(0), HIGH(1);
// getters
}
I'm trying to make a card game, and have my card class and my deck class sort of ready, it compiles ok, but when I try to run deck's method makeDeckFull, i get the output: invalidnumberinvalidnumber...
when I use the showDeck method I then see this instead of "hearts", 1
Cards#597f13c5 (i do not know what it means, or how to fix it)
Any help would be kindly appreciated: code below.
Deck Class:
import java.util.ArrayList;
public class Deck
{
private ArrayList<Cards> deck;
private int index;
public Deck()
{
deck = new ArrayList<Cards>();
}
public void makeDeckFull()
{
Cards h1 = new Cards("Hearts", 1);
Cards h2 = new Cards("Hearts", 2);
Cards h3 = new Cards("Hearts", 3);
deck.add(h1);
index ++;
deck.add(h2);
index ++;
deck.add(h3);
index ++;
//Rest of these is left out to conserve space
}
public void showDeck()
{
System.out.println(deck);
}
Card class:
public class Cards
{
private String HEARTS = "Hearts";
private String CLUBS = "Clubs";
private String DIAMONDS = "Diamonds";
private String SPADES = "Spades";
public int number;
public String suit;
public Cards()
{
suit = "unknown suit";
number = 0;
}
public Cards(String suit, int number)
{
setSuit(suit);
setNumber(number);
}
public void setCard(String suit, int number2)
{
setSuit(suit);
setNumber(number2);
}
public void setSuit(String newSuit)
{
if(
(newSuit.equalsIgnoreCase(HEARTS)) ||
(newSuit.equalsIgnoreCase(DIAMONDS)) ||
(newSuit.equalsIgnoreCase(CLUBS)) ||
(newSuit.equalsIgnoreCase(SPADES)))
{
suit = newSuit;
}
else
{
newSuit = "invalid";
System.out.print("Invalid");
}
}
public int getNumber()
{
return number;
}
public String getSuit()
{
return suit;
}
public void setNumber(int newNumber)
{
if(newNumber >0 && newNumber <=10)
{
number = newNumber;
}
else
{
number = 0;
System.out.print("invalid number");
}
}
}
1) You need to override toString() in the Cards class. As is, you are printing out the reference of the object(the gibberish) instead of the "data." You should also override the toString() method of Deck to only print out the list.
2) I'm stepping through your code snippet of makeDeckFull(), and it seems to work fine. Are you sure those three inserts are where you are getting the invalid print statements?
ok so my assignment I'm supposed to write a class that stores a temperature that the user gives and checks it with the set parameters to see if Ethy/Oxygen/Water are either freezing or boiling and then display it at the end which ones will be freezing/boiling at the temperature that they entered. I have the majority of both the class and tester completed but I'm getting several errors on my code. I'm not asking anyone to give me the answer but if you could tell me what I'm doing wrong I would greatly appreciate it. Here is my code for class:
public class FreezingBoilingPoints {
private int temperature;
public FreezingBoilingPoints(int temp) {
temperature = temp;
}
public void setTemperature(int temp) {
temperature = temp;
}
public int getTemperature() {
return temperature;
}
private Boolean isEthylFreezing(int temperature) {
if (temperature <= -173) {
return true;
} else {
return false;
}
}
private Boolean isEthylBoiling(int temperature) {
if (temperature >= 172) {
return true;
} else {
return false;
}
}
private Boolean isOxygenFreezing(int temperature) {
if (temperature <= -362) {
return true;
} else {
return false;
}
}
private Boolean isOxygenBoiling(int temperature) {
if (temperature >= -306) {
return true;
} else {
return false;
}
}
private Boolean isWaterFreezing(int temperature) {
if (temperature <= 32) {
return true;
} else {
return false;
}
}
private Boolean isWaterBoiling(int temperature) {
if (temperature >= 212) {
return true;
} else {
return false;
}
}
public String showTempinfo() {
if (isEthylFreezing()) {
System.out.println("Ethyl will freeze");
}
if (isEthylBoiling()) {
System.out.println("Etheyl will boil");
}
if (isOxygenFreezing()) {
System.out.println("Oxygen will freeze");
}
if (isOxygenBoiling()) {
System.out.println("Oxygen will Boil");
}
if (isWaterFreezing()) {
System.out.println("Water will freeze");
}
if (isWaterBoiling()) {
System.out.println("Water will boil");
}
}
}
and the code for my tester is below:
import java.util.Scanner;
public class FreezingBoilingTester {
public static void main(String[] args) {
int temperature;
FreezingBoilingPoints temp1 = new FreezingBoilingPoints(0);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a temperature: ");
temperature = scan.nextInt();
System.out.println(showTempinfo());
}
}
1) don't pass the temp inside methods, because you already have this value in member variable.
2) you can change if (condition) then true else false into return (condition) and it will be the same result, just for readability .
3) you should return boolean not Boolean wrapper until you need the wrapper.
public final class FreezingBoilingPoints {
private int temperature;
public FreezingBoilingPoints(int temp) {
temperature = temp;
}
public void setTemperature(int temp) {
temperature = temp;
}
public int getTemperature() {
return temperature;
}
private boolean isEthylFreezing() {
return (temperature <= -173);
}
private boolean isEthylBoiling() {
return (temperature >= 172);
}
private boolean isOxygenFreezing() {
return (temperature <= -362);
}
private boolean isOxygenBoiling() {
return (temperature >= -306);
}
private boolean isWaterFreezing() {
return (temperature <= 32) ;
}
private boolean isWaterBoiling() {
return (temperature >= 212);
}
public String showTempinfo() {
StringBuilder result = new StringBuilder();
if (isEthylFreezing()) {
result.append("Ethyl will freeze");
result.append("\n");
}
if (isEthylBoiling()) {
result.append("Etheyl will boil");
result.append("\n");
}
if (isOxygenFreezing()) {
result.append("Oxygen will freeze");
result.append("\n");
}
if (isOxygenBoiling()) {
result.append("Oxygen will Boil");
result.append("\n");
}
if (isWaterFreezing()) {
result.append("Water will freeze");
result.append("\n");
}
if (isWaterBoiling()) {
result.append("Water will boil");
result.append("\n");
}
return result.toString();
}
}
Main:
import java.util.Scanner;
public class FreezingBoilingTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a temperature: ");
int temperature = scan.nextInt();
FreezingBoilingPoints temp1 = new FreezingBoilingPoints(temperature );
System.out.println(temp1.showTempinfo());
}
}
updated:
you can use String concatenation:
String result = "";
if ( condition ) {
result += "new result";
result += "\n";
}
but this is not recommended in term of performance, because each += operation will create another String object in memory holding the new result.
The problem is that your private methods are taking in a temperature and yet, you are not passing one in for your showTempinfo() method. Try removing the input parameters and using the temp set in the class. Also, you need to somehow set the temp before you call showTempinfo().
Hope this helps.
You're not passing the input that the user is giving you into the constructor for your FreezingBoilingPoints class. You're initializing that class with 0 and then asking for a temperature from the user. There's no relationship between the temperature the user provided and the class that you're using to test it.
You need to construct your FreezingBoilingPoints object in your main method, then call showTempinfo() on it. Also, your private calc methods should use the member variable; there's no need to take it as a parameter.
You need to pass the user input, temperature, into your FreezingBoilingPoints constructor. Also, the method showTempInfo() is instance specific. For example, you need to instantiate your object, temp1, by passing the user input with the constructor and then invoke temp1.showTempInfo()
Here we go:
1) All your "is..." methods are expecting for an int parameter, however when you're calling them, you're not passing anything. Remove the int parameter from either the method implementation or the method calls
2) You're missing a closing bracket for the method isWaterBoiling;
3) You marked the method "showTempinfo" as returning String, but you are not returning anything for that method. Either add the return command or remove the "String" from the method signature.
In your showTempinfo(), you try to do isEthylFreezing().
But it can't work ... isEthylFreezing is waiting for an int ... but it gets nothing ...