public class Patient{ private String name; private int age; private double weight; /** Gender of patient. 'M' for male and 'F' for female.*/ private char gender; private double bloodPressure; /** These are true if the patient is allergic to the drug in question. */ private boolean allergicToAbocin; private boolean allergicToBorocin; private boolean allergicToCitocin; public boolean canTakeAbocin() {}; public boolean canTakeBorocin() {}; public boolean cantakeCitocin() {}; }implement the three functions canTakeAbocin, canTakeBorocin, canTakeCitocin making sure to obey all the following rules:
public boolean canTakeAbocin() { if (allergicToAbocin == false) { if (this.age < 13) { if (this.weight < (this.age * 10)) { return true; } } } return false; } public boolean canTakeBorocin() { if (allergicToBorocin == true) return false; if(this.gender == ‘F’ && this.age >= 60) return false; if(this.gender == ‘M’ && this.age > 65) { if(this.bloodpressure > 10) { if (this.weight >= 200) return false; } } return true; } public boolean canTakeCitocin() { if (allergicToCitocin == true || canTakeBorocin == true) return false; return true; }
char
s 'a' 'l' 'e' 'x' to form the String
"alex". String s = ""; s += "a"; s += "l"; s += "e"; s += "x";
final
keyword used for? The final keyword is used to make a variable only take on one value. The first value is the last value that is ever assigned to the variable. Stooge
. Show your code.
public class stooge{ public static int num = 0; public void stooge( ) { if (num < 4) { super( ); num++; } else //We actually want to throw an Exception here, but we have not covered // throw yet. System.out.println("There are too many stooge’s"); }
x = y
do when both
variables are of a primitive type. x will get the value of y.x = y
do when both
variables are of a non-primitive type. x will reference the same memory spot of y.public static void main(String args[]){ String s = "01223334444566"; Distribution.doDistribution(s); } }should produce the following output:
0:* 1:* 2:** 3:*** 4:**** 5:* 6:** 7: 8: 9:Tip: The following for loop
for (char c = '0'; c <= '9' ; c++)iterates over the characters '0', '1', '2', ... '9'. Also, check page 350 of the textbook for the needed String functions. Solution:
public class Distribution{ public static void doDistribution(String s){ for (char c = '0'; c <= '9' ; c++){ int index = 0; int count = 0; while ((index = s.indexOf(c,index)) != -1){ count++; index++; } System.out.print(c + ":"); for (;count > 0; count--){ System.out.print("*"); } System.out.println(); } } public static void main(String args[]){ String s = "01223334444566"; Distribution.doDistribution(s); } }