0% found this document useful (0 votes)
9 views9 pages

4.3 Practice Java

The document provides Java programming exercises that include calculating the area of a triangle, implementing various mathematical formulas, determining the number of buses needed for a field trip, and evaluating boolean expressions. It also discusses incorrect variable declarations and conventions in Java programming. Each section includes code examples and explanations for the exercises.

Uploaded by

harilavgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

4.3 Practice Java

The document provides Java programming exercises that include calculating the area of a triangle, implementing various mathematical formulas, determining the number of buses needed for a field trip, and evaluating boolean expressions. It also discusses incorrect variable declarations and conventions in Java programming. Each section includes code examples and explanations for the exercises.

Uploaded by

harilavgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CSA0961-JAVA

PRACTICE- 4.2

1. Write a program that will take in the base and height of a triangle and calculate and display

the area of the triangle using the formula below. 𝐴𝐴 = 1 2 𝑏𝑏ℎ

CODE:

import java.util.Scanner;

public class TriangleArea

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the base of the triangle: ");

double base = scanner.nextDouble();

System.out.print("Enter the height of the triangle: ");

double height = scanner.nextDouble();

double area = 0.5 * base * height;

System.out.println("The area of the triangle is: " + area);

}
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

been declared and initialized.

a. 𝑎𝑎 = √𝑥𝑥5−6 4

b. 𝑏𝑏 = 𝑥𝑥𝑦𝑦 − 6𝑥𝑥

c. 𝑐𝑐 = 4𝑐𝑐𝑐𝑐𝑐𝑐( 𝑧𝑧 5 ) − 𝑠𝑠𝑠𝑠𝑠𝑠𝑥𝑥2

d. 𝑑𝑑 = 𝑥𝑥4 − �6𝑥𝑥 − 𝑦𝑦3

e. 𝑒𝑒 = 1 𝑦𝑦− 1 𝑥𝑥−2𝑦𝑦

f. 𝑓𝑓 = 7(𝑐𝑐𝑐𝑐𝑐𝑐(�5 − 𝑠𝑠𝑠𝑠𝑠𝑠√3𝑥𝑥 − 4))

code:

public class MathFormulas {

public static void main(String[] args) {

if (args.length < 3) {

System.out.println("Please provide the values for x, y, and z as arguments.");


return;

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)

double c = 4 * Math.cos(z / 5) - Math.sin(Math.pow(x, 2));

// d = x^4 - √(6x - y^3)

double d = Math.pow(x, 4) - Math.sqrt(6 * x - Math.pow(y, 3));

// e = 1 / (y - 1 / (x - 2y))

double e = 1 / (y - 1 / (x - 2 * y));

// f = 7 * cos(√(5 - sin(√(3x - 4))))

double f = 7 * Math.cos(Math.sqrt(5 - Math.sin(Math.sqrt(3 * x - 4))));

System.out.println("a = " + a);


System.out.println("b = " + b);

System.out.println("c = " + c);

System.out.println("d = " + d);

System.out.println("e = " + e);

System.out.println("f = " + f);

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

number of people that will need to ride in vans.


Code:

public class FieldTrip {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("Please provide the number of people signed up for the field trip

as an argument.");

return;

int people = Integer.parseInt(args[0]);

int busCapacity = 45;

int busesNeeded = people / busCapacity;

int peopleInVans = people % busCapacity;

System.out.println("Number of buses needed: " + busesNeeded);

System.out.println("Number of people that will need to ride in vans: " + peopleInVans);

}
4. 4. Write true or false on the blanks in the program below to show the value of the boolean

variable true_false as the program executes.

Code:

public class TrueFalse {

public static void main(String[] args) {

int i = 5;

int j = 6;

boolean true_false;

true_false = (j < 5); // false


System.out.println(true_false);

true_false = (j > 3); // true

System.out.println(true_false);

true_false = (j < i); // false

System.out.println(true_false);

true_false = (i < 5); // false

System.out.println(true_false);

true_false = (j <= 5); // false

System.out.println(true_false);

true_false = (6 < 6); // false

System.out.println(true_false);

true_false = (i != j); // true

System.out.println(true_false);

true_false = (i == j || i < 50); // true

System.out.println(true_false);

true_false = (i == j && i < 50); // false

System.out.println(true_false);

true_false = (i > j || true_false && j >= 4); // true

System.out.println(true_false);

true_false = (!(i < 2 && j == 5)); // true

System.out.println(true_false);

true_false = !true_false; // false

System.out.println(true_false);

}
5. 5. Explain why each of the declarations in the second list are wrong.

1.boolean gameOver = false;

int students=50,classes=3;

double sales_tax;

short number1;

2. int 2beOrNot2be;

float price index;

double lastYear'sPrice;

long class;

ANS:

 int 2beOrNot2be;

 Variable names cannot start with a digit.


 float price index;

 Variable names cannot contain spaces.

 double lastYear'sPrice;

 Variable names cannot contain apostrophes.

 long class;

 class is a reserved keyword in Java and cannot be used as a variable name.

6. Explain Why Declarations Do Not Follow Conventions

1. int cadence=3, speed=55, gear=4;

o Multiple variables should be declared on separate lines for readability.

2. final double SALES_TAX=.06;

o Constants should be in uppercase, but underscores should be used to separate

words for readability (e.g., SALES_TAX).

3. double gearRatio=.5;

o Mixed case is acceptable, but the Java convention is to use camelCase (e.g.,

gearRatio).

4. int currentGear=5;

o This follows conventions.

5. int c=3, s=55, g=4;

o Variable names should be descriptive (e.g., cadence, speed, gear).

6. final double salesTax=.06;

o Constants should be in uppercase and use underscores to separate words (e.g.,

SALES_TAX).

7. double gearrat

o Variable names should use camelCase and be descriptive (e.g., gearRatio).

You might also like