Hrs Comp Prog Ch 3 Exam Prep

result = 15 % 4;

3

result = 7 / 3 + 2;

4

result = 2 + 7 * 5;

37

result = 45 / 8 * 4 + 2;

22

result = 17 + (21 % 6) * 2;

23

result = ((int)(4.5 + 2.6 * 0.5)

5

dm = m * Math.sqrt(1 + v / c) / (Math.sqrt(1 - v / c0 - 1); in algebraic notation is:

dm = m(?1 + v/c) / (?1 - v/c ) - 1

volume = 4
Math.PI
Math.pow(r, 3) / 3; in algebraic notation is:

volume = 4?r^3 / 3

Math.abs(-9.1) =

9.1

Math.sqrt(49.0) =

7.0

((int)Math.round(6.51)) =

7

Math.pow(4, 2) =

16.0

public class TriangleTest
{
public static void main(String[] args)
{
. . .
final int NUM_SIDES = 3;
System.out.println("A triangle has " + NUM_SIDES + " sides.");
}
}
What is the output? What is NUM_SIDES?

Output: A triangle has 3 sides.
NUM_SIDES: constant variable with a valuable of 3

In the statement:
System.out.println( );
System = ?
out = ?
println = ?

System = class
out = object
println = method

Example of a Class?

Review

Example of an object?

question

Example of a method?

showInputDialog( );

You cast when you convert an int to a double

False

You cast when you convert one type to another

True

You cast when you perform a conversion that loses information

True

You cast when you convert a String to an int

False

Java code asking for age using JOptionPane and convert to int

String input = JOptionPane.showInputDialog("Enter your age");
int age = Integer.parseInt(input);

Write java code for school using JOptionPane and school population. convert population to int

String school = JOptionPane.showInputDialog("Enter school name");
String pop = JOptionPane.showInputDialog("Enter poulation");
int population = Integer.parseInt(pop);

Given:
String input = "125.75"
int count = Integer.parseInt(input);
System.out,println(count);
What will happen if code is run?

Compiler error. Can't turn a decimal into an integer

Given:
double firstNum = 10;
double secondNum = firstNum;
secondNum = secondNum + 30;
What is final value of firstNum?

final value = 10

Given:
String message = "Java Rules";
write statement for all lowercase and uppercase

L: System.out.println("Lowercase: " + message.toLowerCase( ));
U: System.out.println("Uppercase: " + message.toUpperCase( ));

Write java statement for integer constant called IQ set to 100

final in IQ = 100

Given:
int a = 45;
int b = 63;
double c = 7.6463
Output:
xx45xxxx63
x7.65 (X = space)
Printf for first line? Second line?

1st line: System.out.printf("%4d%6d%n", a, b);
2nd line: System.out.printf("%5.2f%n", c);

Given:
String name1 = " . . . ";
String name2 = ". . . ";
int age1 = . . . ;
int age2 = . . . ;
create String str with left justified name width of 10, and right justified age width of 2, followed by newline character.
create 2 printf statements

String str = "%-10s%2d%n"0;
System.out.println(str, name1, age1);
System.out.println(Str,name2, age2);