4.3 Practice Java
4.3 Practice Java
PRACTICE- 4.2
1. Write a program that will take in the base and height of a triangle and calculate and display
CODE:
import java.util.Scanner;
}
2. Write the following math formulas in Java. You will need to use methods from the Math
class as well as nesting of methods and parentheses to force the order of operations to
correctly calculate the answer. Assume that all the variables in the formulas have already
a. 𝑎𝑎 = √𝑥𝑥5−6 4
b. 𝑏𝑏 = 𝑥𝑥𝑦𝑦 − 6𝑥𝑥
c. 𝑐𝑐 = 4𝑐𝑐𝑐𝑐𝑐𝑐( 𝑧𝑧 5 ) − 𝑠𝑠𝑠𝑠𝑠𝑠𝑥𝑥2
e. 𝑒𝑒 = 1 𝑦𝑦− 1 𝑥𝑥−2𝑦𝑦
code:
if (args.length < 3) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]);
// a = √(x^5 - 6) / 4
double a = Math.sqrt(Math.pow(x, 5) - 6) / 4;
// b = xy - 6x
double b = x * y - 6 * x;
// c = 4cos(z / 5) - sin(x^2)
// e = 1 / (y - 1 / (x - 2y))
double e = 1 / (y - 1 / (x - 2 * y));
3.bus holds 45 people. The school will only use a bus if they can fill it completely. The rest of the
people will ride in vans. Write a program that will take in the number of people that are signed
up to go on a field trip. Have the program print the number of busses necessary and then total
if (args.length < 1) {
System.out.println("Please provide the number of people signed up for the field trip
as an argument.");
return;
}
4. 4. Write true or false on the blanks in the program below to show the value of the boolean
Code:
int i = 5;
int j = 6;
boolean true_false;
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
System.out.println(true_false);
}
5. 5. Explain why each of the declarations in the second list are wrong.
int students=50,classes=3;
double sales_tax;
short number1;
2. int 2beOrNot2be;
double lastYear'sPrice;
long class;
ANS:
int 2beOrNot2be;
double lastYear'sPrice;
long class;
3. double gearRatio=.5;
o Mixed case is acceptable, but the Java convention is to use camelCase (e.g.,
gearRatio).
4. int currentGear=5;
SALES_TAX).
7. double gearrat