Using Classes and Objects
Using Classes and Objects
Using Classes and Objects
String title;
count = title.length()
num1 38
num1 38
Before:
num2 96
num2 = num1;
num1 38
After:
num2 38
name2 = name1;
• Or you can import the class, and then use just the
class name
import java.util.Scanner;
import java.lang.*;
• Let’s look:
num1 = generator.nextInt(10); // look up nextInt(10) (p. 125) Get used to doing this…
System.out.println ("From 0 to 9: " + num1);
num1 = generator.nextInt(10) + 1;
System.out.println ("From 1 to 10: " + num1);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
• These include:
absolute value
square root
exponentiation
trigonometric functions
getPercentInstance()
System.out.print (“Enter the unit price: “); // another poor prompt. Why?
unitPrice = scan.nextDouble();
subtotal- quantity*unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax; // have computed a subtotal, tax, and a total cost
Outputs:
int num;
num = Integer.parseInt(str);
Or
float fltnum;
fltnum = Float.parseFloat(str);