int min1 = (n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3;
int min1 = ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4;
int min1 = (((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4) < n5 : (((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4) : n5;
Area of a square: A = s2 Area of a circle: A = pir2 Circumference of a circle: C = 2pir Positive root of the quadratic equation *: * Slope of the line between two points: *
double A = s*s;
double A = 3.141592 * r * r;
double C = 2 * 3.141592 * r;
double root = (-b + Math.sqrt(b*b - 4*a*c))/(2 * a);
double slope = (Y2 - Y1)/(X2 - X1);
The quadratic equation was an unfair question since you haven't yet seen the
Math.sqrt() method. It's coming in Chapter 8. In that chapter you'll also
learn that you could use the double constant Math.PI instead of the literal
3.141592 to represent the value of pi.
class salestax {
public static void main (String[] args) {
double price = Double.valueOf(args[0]).doubleValue();
double salestax = price * 0.0825;
System.out.println("Sales tax is " + salestax);
}
}
If you know about exceptions, you can make this example somewhat more robust against bad user input as follows:
class salestax {
public static void main (String[] args) {
try {
double price = Double.valueOf(args[0]).doubleValue();
double salestax = price * 0.0825;
System.out.println("Sales tax is " + salestax);
}
catch (NumberFormatException e) {
System.err.println("Usage: java salestax price" );
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Usage: java salestax price" );
}
catch (Exception e) {
System.err.println(e);
}
}
}
class salestax {
public static void main (String[] args) {
double total = 0;
double price;
int i = 0;
while (i < args.length) {
price = Double.valueOf(args[i]).doubleValue();
total = total + price;
i = i + 1;
}
double salestax = total * 0.0825;
System.out.println("Sales tax is " + salestax);
}
}
class payday {
public static void main (String[] args) {
double hours = Double.valueOf(args[0]).doubleValue();
double rate = Double.valueOf(args[1]).doubleValue();
double pay = rate * hours;
System.out.println("The paycheck is " + pay + " dollars.");
}
}
class payday {
public static void main (String[] args) {
double hours = Double.valueOf(args[0]).doubleValue();
double rate = Double.valueOf(args[1]).doubleValue();
double pay;
if (hours > 40) {
pay = rate * 40 + 1.5 * rate * (hours - 40);
}
else {
pay = rate * hours;
}
System.out.println("The paycheck is " + pay + " dollars.");
}
}
class payday {
public static void main (String[] args) {
double hours = Double.valueOf(args[0]).doubleValue();
double rate = Double.valueOf(args[1]).doubleValue();
double pay;
if (hours > 40) {
pay = rate * 40 + 1.5 * rate * (hours - 40);
}
else {
pay = rate * hours;
}
System.out.println("The paycheck is " + pay + " dollars.");
if ( rate < 4.35) {
System.err.println("This employee is not getting the legally required minimum wage.");
}
if ( hours > 7*24) {
System.err.println("Did this employee really work " + hours + " hours?");
}
}
}
class intocm {
public static void main (String[] args) {
double inches = Double.valueOf(args[0]).doubleValue();
double cm = inches * 2.54;
System.out.println(cm + " centimeters");
}
}
class cmtoin {
public static void main (String[] args) {
double cm = Double.valueOf(args[0]).doubleValue();
double inches = cm / 2.54;
System.out.println(inches + " inches");
}
}
class lbstokg {
public static void main (String[] args) {
double lbs = Double.valueOf(args[0]).doubleValue();
double kg = lbs * 454.0/1000.0;
System.out.println(kg + " kg");
}
}
class kgtolbs {
public static void main (String[] args) {
double kg = Double.valueOf(args[0]).doubleValue();
double lbs = kg * 1000.0/454.0;
System.out.println(kg + " kg");
}
}
to make this one program you need a way of telling whether the number entered on the command line is in pounds or kilograms. Let's adopt the convention that if the number is given in kilograms, then the second command line argument will bekg. Thus
class lbkg {
public static void main (String[] args) {
if (args[1].equals("kg")) {
double kg = Double.valueOf(args[0]).doubleValue();
double lbs = kg * 1000.0/454.0;
System.out.println(kg + " kg");
}
else {
double lbs = Double.valueOf(args[0]).doubleValue();
double kg = lbs * 454.0/1000.0;
System.out.println(kg + " kg");
}
}
}
class seriesResistance {
public static void main (String[] args) {
double total = 0;
double resistance;
int i = 0;
while (i < args.length) {
resistance = Double.valueOf(args[i]).doubleValue();
total = total + resistance;
i = i + 1;
}
System.out.println("The total resistance is " + total);
}
}
If there are more than two resistors you continue to invert the sum of their inverses; e.g. for four resistors you have:
Write a program that calculates the resistance of a a group of resistors arranged in parallel.
class parallelResistance {
public static void main (String[] args) {
double total = 0;
double resistance;
int i = 0;
while (i < args.length) {
resistance = 1.0/Double.valueOf(args[i]).doubleValue();
total = total + resistance;
i = i + 1;
}
total = 1.0/total;
System.out.println("The total resistance is " + total);
}
}