new in java method and return - java

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
}

Related

How can I compare two strings and return the lexicographical ordered result using the compareTo method?

I am trying to use compareTo method to compare two different names. After running the first attempt the program terminates immediately without returning anything. How can I modify this compareTo method to compare the names (Name n and Name n2) and return the result (-1, 1 or 0)? And obviously a print statement can be added to display (equal, before , or after) for the comparison. Thanks for any assistance.
//First attempt
public class Name implements Comparable<Name> {
private String Name;
public Name(String string) {
// TODO Auto-generated constructor stub
}
public String getName() {
return Name;
}
public int compareTo(Name other) {
if (getName().compareTo(other.getName()) < 0) {
return -1;
} else if (getName().compareTo(other.getName()) > 0) {
return 1;
} else if (getName().equals(other.getName())) {
return 0;
}
return getName().compareTo(other.getName());
}
public static void main(String[] args) {
Name n = new Name("jennifer");
n.getName();
Name n2 = new Name("paul");
n2.getName();
}
}
//second attempt
public class Name implements Comparable<String> {
private String Name;
public String getName() {
return Name;
}
public int compareTo(String other) {
if (getName().compareTo(other.getName()) < 0) {
return -1;
} else if (getName().compareTo(other.getName()) > 0) {
return 1;
} else if (getName().equals(other.getName())) {
return 0;
}
return getName().compareTo(other.getName());
}
public static void main(String[] args) {
String Name = new String("jennifer");
String other = new String("paul");
}
}
//First attempt
public class Name {
public static void main(String[] args) {
String n = new String("jennifer");
String n2 = new String("paul");
if (n.compareTo(n2) < 0) {
System.out.println(n +" is before than " +n2);
} else if (n.compareTo(n2) > 0) {
System.out.println(n +" is after than " +n2);
} else if (n.compareTo(n2) == 0) {
System.out.println(n +" is equals to " +n);
}
}
}
Outoput:
jennifer is before than paul
By the way, check this out because every programming language has its own set of rules and conventions and for variables in Java is like this:
If the name you choose consists of only one word, spell that word in
all lowercase letters. If it consists of more than one word,
capitalize the first letter of each subsequent word.
public class Name implements Comparable<Name> {
private String name;
public Name(String name) {
this.name=name;
}
public String getName() {
return name;
}
public int compareTo(Name other) {
if (getName().compareTo(other.getName()) < 0) {
return -1;
} else if (getName().compareTo(other.getName()) > 0) {
return 1;
} else if (getName().equals(other.getName())) {
return 0;
}
return getName().compareTo(other.getName());
}
public static void main(String[] args) {
Name n = new Name("jennifer");
n.getName();
Name n2 = new Name("paul");
n2.getName();
System.out.println(n.getName());
System.out.println(n2.getName());
System.out.println(n2.compareTo(n));
}
}
OUTPUT :
jennifer
paul
1

How to use the Comparator interface in Java?

I can't figure out how to properly use the Comparator interface.
NOTE: I don't want to use the "one line" comparator implementation I see on most code, meaning :
Collections.sort(deck, new Comparator<Card>() {
#Override
public int compare(Card o1, Card o2) {
return WHATEVER;
});
Again, I don't want to use this, because I'm going to be sorting many different ArrayLists and it seems like a waste of space to do that every time. I want to somehow make it work, having my compareTo method written once in one place.
I tried many different things, but I'm new to implementing interfaces, so I'm probably missing something.
This is my Card class with it's compare method. (NOTE: Let me save you some time and say that I'm not interested in using Enumerations to simplify the code and the comparison, I just want to learn how to use Comparator properly. I know it looks bad but I have to use some already written code. )
import java.util.*;
public class Card implements Comparator<Card>
{
private String number;
private int value;
private int finalValue;
private String suit;
private Player owner;
public Card(String number, String suit)
{
this.number=number;
switch (number){
case "2": this.value = 2;
break;
case "3": this.value = 3;
break;
case "4": this.value = 4;
break;
case "5": this.value = 5;
break;
case "6": this.value = 6;
break;
case "7": this.value = 7;
break;
case "8": this.value = 8;
break;
case "9": this.value = 9;
break;
case "10": this.value = 10;
break;
case "J": this.value = 11;
break;
case "Q": this.value = 12;
break;
case "K": this.value = 13;
break;
case "A": this.value = 14;
break;
}
this.suit=suit;
switch (suit){
case "Spades": this.finalValue = this.value*4;
break;
case "Hearts": this.finalValue = this.value*5;
break;
case "Clubs": this.finalValue = this.value*2;
break;
case "Diamonds": this.finalValue = this.value*3;
break;
}
}
public int compare(Card card1, Card card2)
{
Integer suitValue1;
if (card1.getSuit() == "Hearts")
suitValue1=5;
else if (card1.getSuit() == "Spades")
suitValue1=4;
else if (card1.getSuit() == "Diamonds")
suitValue1=3;
else
suitValue1=2;
Integer suitValue2;
if (card2.getSuit() == "Hearts")
suitValue2=5;
else if (card2.getSuit() == "Spades")
suitValue2=4;
else if (card2.getSuit() == "Diamonds")
suitValue2=3;
else
suitValue2=2;
Integer value1, value2;
value1 = card1.getValue();
value2 = card2.getValue();
if (suitValue1 != suitValue2)
return suitValue1.compareTo(suitValue2);
else
return value1.compareTo(value2);
}
//get methods
public int getValue()
{return this.value;}
public int getFinalValue()
{return this.finalValue;}
public String getNumber()
{return this.number;}
public String getSuit()
{return this.suit;}
public Player getOwner()
{return this.owner;}
//set methods
public void setValue(int value)
{this.value = value;}
public void setFinalValue(int finalValue)
{this.finalValue = finalValue;}
public void setNumber(String number)
{this.number = number;}
public void setSuit(String suit)
{this.suit = suit;}
public void setOwner(Player player)
{this.owner = player;}
}
So that's my compare method. Should I put it in another class or something? I try to call it using this:
Collections.sort(deck, new Comparator<Card>());
but that is obviously wrong.
deck is an ArrayList : private ArrayList<Card> deck = new ArrayList<Card>(); filled with various Card objects.
You are confusing Comparable with Comparator. They are similar of course but really fundamentally different.
Comparable defines the natural ordering of a class--how instances of your class should sort themselves by default. That's actually what you want here.
Comparator helps you to sort the instances of your class when you want to use a different sort order than the natural order. That doesn't seem to be your aim here.
So what you actually want is this:
public class Card implements Comparable<Card> {
public int compareTo(Card otherCard) {
//return 1 when this instance is greater than the parameter, -1 if less; 0 if equal
}
//Other stuff
//And make sure compareTo is consistent with equals
}
Note that the method you have to implement is compareTo, which is different from what you implement with Comparator.
Then you simply sort this way:
Collections.sort(deck)
And the cards will know how to sort themselves.
If you need more guidance, we actually did a tutorial on Comparable and Comparator with the files available on Github. Check it out if you like.
Since you seem to have one only way to compare Card objects, you don't really have to use a Comparator. Let Card implement Comparable, instead, and then sort by the 1-parameter method:
Collections.sort(deck);
To summarize:
To sort the array use Collections.sort(deck);
Your Card class needs to implement Comparable
When comparing Strings its best to use equals
It is better to use the constant String first ie "Hearts".equals(this.getSuit()), this will guard against Null pointers
You want to implement Comparable, and then you can just sort the array.
I agree with Juned, use value1.equals(value2) this returns a boolean. From the looks of things you only doing string comparison which is fairly stright forward.
Good luck.
i think this is what you intended. I don't believe you can use a switch statement on Strings unless you are 1.7+
public class Card implements Comparable<Card>
{
private String number;
private int value;
private int finalValue;
private String suit;
private Player owner;
public Card(String number, String suit)
{
this.number = number;
this.value = 0;
this.finalValue = 0;
//card value
if ("2".equalsIgnoreCase(number))
{
this.value = 2;
}
else if ("3".equalsIgnoreCase(number))
{
this.value = 3;
}
else if ("4".equalsIgnoreCase(number))
{
this.value = 4;
}
else if ("5".equalsIgnoreCase(number))
{
this.value = 5;
}
else if ("6".equalsIgnoreCase(number))
{
this.value = 6;
}
else if ("7".equalsIgnoreCase(number))
{
this.value = 7;
}
else if ("8".equalsIgnoreCase(number))
{
this.value = 8;
}
else if ("9".equalsIgnoreCase(number))
{
this.value = 9;
}
else if ("10".equalsIgnoreCase(number))
{
this.value = 10;
}
else if ("J".equalsIgnoreCase(number))
{
this.value = 11;
}
else if ("Q".equalsIgnoreCase(number))
{
this.value = 12;
}
else if ("K".equalsIgnoreCase(number))
{
this.value = 13;
}
else if ("A".equalsIgnoreCase(number))
{
this.value = 14;
}
//suits
if ("Spades".equalsIgnoreCase(number))
{
this.finalValue = this.value * 4;
}
else if ("Hearts".equalsIgnoreCase(number))
{
this.finalValue = this.value * 5;
}
else if ("Clubs".equalsIgnoreCase(number))
{
this.finalValue = this.value * 2;
}
else if ("Diamonds".equalsIgnoreCase(number))
{
this.finalValue = this.value * 3;
}
}
#Override
public int compareTo(Card o)
{
final int EQUAL = 0;
final int LESS_THAN = -1;
final int GREATER_THAN = 1;
if (this == o || this.getFinalValue() == o.getFinalValue())
{
return EQUAL;
}
else if (this.getFinalValue() < o.getFinalValue())
{
return LESS_THAN;
}
else
{
return GREATER_THAN;
}
}
// get methods
public int getValue()
{
return this.value;
}
public int getFinalValue()
{
return this.finalValue;
}
public String getNumber()
{
return this.number;
}
public String getSuit()
{
return this.suit;
}
public Player getOwner()
{
return this.owner;
}
// set methods
public void setValue(int value)
{
this.value = value;
}
public void setFinalValue(int finalValue)
{
this.finalValue = finalValue;
}
public void setNumber(String number)
{
this.number = number;
}
public void setSuit(String suit)
{
this.suit = suit;
}
public void setOwner(Player player)
{
this.owner = player;
}
}

Having problems with my Card Game in java

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(...).

Replace String Literals If/elseIf block with Enum

I'm new to using Java Enums and I've read that replace IF logic that compares String literals should be replaced with an Enum. I don't quite understand how to replace my below code with an Enum, any ideas? Based on the col value being passed into applyEQ, I need to do a base the next method call on it's value. I do know the possible values of col ahead of time and I'm using a constants file for now. Should I create an Enum and place it in my Interface of Constants file?
public class FilterHelper implements IFilterHelper {
private final EQuery eQuery;
public FilterHelper(EQuery query) {
eQuery = query;
}
#Override
public void applyEQ(String col, String val) throws Exception {
int return = 0;
if (col.equalsIgnoreCase(EConstants.NAME)) {
ret = Sample.addName(eQuery, val);
} else if (col.equalsIgnoreCase(EConstants.KEYWORDS)) {
ret = Sample.addKey(eQuery, val);
} else if (col.equalsIgnoreCase(EConstants.ROLE)) {
ret = Sample.addRole(eQuery, val);
}
if (return != 0) {
throw new Exception("failed");
}
}
}
EConstants.java
public final class EConstants {
public static final String NAME = "cewName";
public static final String KEYWORDS = "cewKeywords";
public static final String ROLE = "cewRole";
}
First create an enum:
public enum EConstants {
CEWNAME,
CEWROLE,
CEWKEYWORDS;
}
Then convert col String to this enum and use switch:
public void applyEQ(String col, String val) throws Exception {
int ret = 0;
final EConstants constant = EConstants.valueOf(col.toUpperCase());
switch(constant) {
case CEWNAME:
ret = Sample.addName(eQuery, val);
break;
case CEWROLE:
ret = Sample.addRole(eQuery, val);
break;
case CEWKEYWORDS:
ret = Sample.addKey(eQuery, val);
break;
default:
throw new Exception("Unhandled enum constant: " + constant);
}
}
Note that EConstants.valueOf() can throw IllegalArgumentException if col.toUpperCase() does not match any of constant values.
BTW I hate local variables initialized in multiple places (and break keyword), try extracting method:
final EConstants constant = EConstants.valueOf(col.toUpperCase());
final int ret = processSample(val, constant);
And the method itself:
private int processSample(String val, EConstants constant) throws Exception {
switch(constant) {
case CEWNAME:
return Sample.addName(eQuery, val);
case CEWROLE:
return Sample.addRole(eQuery, val);
case CEWKEYWORDS:
return Sample.addKey(eQuery, val);
default:
throw new Exception("Unhandled enum constant: " + constant);
}
}
You can rewrite your EConstants as enum:
public enum EConstants {
NAME, KEYWORDS, ROLE
}
And evaluate condition using switch statement:
// col has type of EConstants
switch (col) {
case NAME:
// do something
break;
case KEYWORDS:
// do something
break;
case ROLE:
// do something
break;
default:
// what to do otherwise
break;
}
The great thing about Java Enums is that they provide language level support for the type safe enum pattern, because among other things it allows you to define methods and even override them. So you could do this:
public enum CewColumn {
NAME("cewName") {
#Override
public int add(EQuery eQuery, String val) {
return Sample.addName(eQuery, val);
}
},
KEYWORDS("cewKeywords") {
#Override
public int add(EQuery eQuery, String val) {
return Sample.addKey(eQuery, val);
}
},
ROLE("cewRole") {
#Override
public int add(EQuery eQuery, String val) {
return Sample.addRole(eQuery, val);
}
};
private final String colName;
private MyColumn(String colName) {
this.colName = colName;
}
private static final Map<String, CewColumn> COLUMNS = new HashMap<>(values().length);
static{
for (CewColumn cewColumn : values()){
COLUMNS.put(cewColumn.colName, cewColumn);
}
}
public abstract int add(EQuery eQuery, String val);
public static CewColumn getCewColumn(String colName){
return COLUMNS.get(colName);
}
}
Then you can use it like this:
CewColumn cewColumn = CewColumn.getCewColumn(colName);
if (cewColumn != null){
int ret = cewColumn.add(eQuery, val);
}
-> You replaced the switch statement with polymorphism!
it is best to create a Enum.
public Enum AvailableCols{
COL_1,
COL_2;
}
and convert the procedure as
public void applyEQ(AvailableCols col, String val) throws Exception {
switch(col){
case COL1:
...
If you still want the string to be preserved you can see the following post
Basically create an enum and change the type of col and use equals() or == to compare the value of col against the enum values. Alternatively you could use a switch statement but I doubt that would make your code more readable for only 3 constants.
Example:
enum EConstants {
NAME,
KEYWORDS,
ROLE;
}
public void applyEQ(EConstants col, String val) throws Exception {
if( col == EConstants.NAME ) {
...
}
....
}
//or
public void applyEQ(EConstants col, String val) throws Exception {
if( EConstants.NAME.equals(col) ) { //col might be null
...
}
....
}
//or
public void applyEQ(EConstants col, String val) throws Exception {
switch( col ) {
case NAME:
...
break;
case ROLE:
...
}
}
http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
If your raw data is a string, you will still need to do a string comparison to assign the enum. This might be faster if you do a lot of comparisons on the result data, but if not, it simply adds complication to your code.
You can iterate over the values of the enum like a collection, which gives you an advantage when you need to add constants. That's not bad.
Here is how to do it:
public enum EConstants {
NAME, KEYWORDS, ROLE
}
...
public EConstants setConstant(String from) {
if (from.equalsIgnoreCase("cewName")) {
return NAME;
} else if (col.equalsIgnoreCase("cewKeywords")) {
return KEYWORDS;
} else if (col.equalsIgnoreCase("cewRole")) {
return ROLE;
}
}
You preprocess your data that way and now when you are trying to figure out logic you can use a switch on the enum type value.
Here is a trick for you. No switch/case (just come up with a better name for EConstants).
public enum EConstants {
NAME,
KEYWORDS,
ROLE;
private interface Applier {
void apply(EQuery query, String val);
}
public void apply(EQuery query, String val) {
map.get(this).apply(query, val);
}
private static Map<EConstants, Applier> map = new HashMap<EConstants, EConstants.Applier>();
static {
map.put(NAME, new Applier() {
#Override
public void apply(EQuery query, String val) {
Sample.addName(query, val);
}
});
map.put(KEYWORDS, new Applier() {
#Override
public void apply(EQuery query, String val) {
Sample.addKey(query, val);
}
});
map.put(ROLE, new Applier() {
#Override
public void apply(EQuery query, String val) {
Sample.addRole(query, val);
}
});
}
}
Now you just write:
#Override
public void applyEQ(EConstants econs, String val) {
econs.apply(equery, val);
}

Having trouble with java class methods

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 ...

Categories

Resources