APCS Ch6
All package identifiers start with a ______ letter and a class identifier starts with a _____ letter
- packages start with lower-case - classes start with upper case
What are 3 steps in creating a Polygon in java?
1) Create a polygon object Polygon penta = new Polygon(); 2) Add coordinates to the object using the addPoint method. penta.addPoint(400,70); penta.addPoint(550,200); penta.addPoint(500,350); penta.addPoint(300,350); penta.addPoint(250,200); 3) Draw the polygon with drawPolygon. g.drawPolygon(penta);
List 7 Math class methods.
1) abs() 2) sqrt() 3) min() 4) max() 5) round() 6) ceil() 7) pow()
Rewrite System.out.println(rand.nextInt(900)+100); so that it uses Math.random.
System.out.println(Math.random()*900)+100);
When creating a Polygon, does the order in which you add the points matter?
YES
How many different shades each of Red, Green, and Blue are possible?
255
What is a wrapper class?.
A class that creates objects capable of storing primitive data values like int and double. e.g. Integer IntObject1 = new Integer(100); The Integer wrapper class named IntObject1 stores the [int] value 100.
What does the word class imply?
A collection of some category
What kind of object is created by this statement: g.setColor(new Color(255,0,255));
A new anonymous color object that sets the color to purple. (red and blue = purple)
Look at program Java0623.java. WHat do you need to do to this program to make the output a hexagon? -------------------------- Polygon penta = new Polygon(); penta.addPoint(400,70); penta.addPoint(550,200); penta.addPoint(500,350); penta.addPoint(300,350); penta.addPoint(250,200); g.drawPolygon(penta);
Add another coordinate point (so that there are 6 total)
Refer to program Java0625.java. How would you change this program to display 500 random lines? -------------------------- Random rndInt = new Random(12345); for (int k = 1; k <= 1000; k++) { int x1 = rndInt.nextInt(800); int y1 = rndInt.nextInt(600); int x2 = rndInt.nextInt(800); int y2 = rndInt.nextInt(600); g.drawLine(x1,y1,x2,y2); }
Alter the condition variable in the for loop to k<=500;
Look at program Java0616.java. What do the 5 zeros mean? ------------------------- DecimalFormat output = new DecimalFormat("00000");
Any number displayed with the format method will be shown with padded leading zeros for any number less than 5 digits
Refer to Java0603.java. Print 2 lines of Java code that will create a Bank object for yourself and initialize both your checking and your savings account to 1$ million.
Bank damond; damond = new Bank(1000000, 1000000);
In the statement Bank tom = new Bank(2000); What is Bank and what is tom?
Bank is the class and tom is the object.
Refer to program Java0602.java. In the statement Bank tom; what is Bank and what is tom?
Bank is the class and tom is the object.
Print the Java code necessary to create a DecimalFormat object called output that will display a real number like 12.34567 as $12.35.
DecimalFormat output = new DecimalFormat("$0.00");
Print the Java code necessary to create a DecimalFormat object called output that will display an integer like 1234567 as 1,234,567.
DecimalFormat output = new DecimalFormat("0,000,000");
Print the Java code necessary to create a DecimalFormat object called output that will display a real number rounded to 3 decimal places.
DecimalFormat output = new DecimalFormat("0.000");
For right now, the author wants you to think of constructing an object as a combination of what 2 things?
Declaring a variable to be an object/class and instantiating the object. e.g. Bank tom = new Bank();
Refer to program Java0604.java. What actions are performed by the closeChecking and closeSavings methods?
It changes both account balances to zero.
How does Java0602.java fix the problem of Java0601.java? ------------------------------------------------------------- Bank tom; tom = new Bank(); Bank sue; sue = new Bank(); tom.checkingDeposit(1000.0); tom.savingsDeposit(5000.0); sue.checkingDeposit(1500.0); sue.savingsDeposit(4000.0); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println();
It creates an object of the Bank class to have proper access.
Look at program Java0611.java. How does this program manage to display random characters? -------------------------- Random rand = new Random(12345); System.out.println( (char) (rand.nextInt(26) + 65) ); System.out.println( (char) (rand.nextInt(26) + 65) );
It generates random integers typecasted to char via their ASCII value.
What does the statement tom = new Bank(); do?
It instantiates a tom object of the bank class. (It allocates the necessary space in memory for the new object and constructs new objects ready for business)
What does the nextInt method of the Random class do?
It returns a random integer value. [as in rand.*nextInt*(#);]
What are the largest and smallest possible int values?
Largest: 2147483647 Smallest: -2147483648
What attributes of the Integer class store the largest and smallest possible int values?
MAX_VALUE and MIN_VALUE
Do you have to import the java.lang package?
No, it is automatically loaded.
Are the same types of objects used by the statement g.setColor(new Color(255,0,255)); used in program Java0622.java? ---------------------- Color myRed = new Color(255,0,64); Color myGreen = new Color(16,255,16); Color myBlue = new Color(64,64,255); g.setColor(myRed); g.setColor(myGreen); g.setColor(myBlue);
No, these are three different, NAMED (not anonymous) objects.
Look at program Java0630.java. What is wrong with this program? -------------------------- Scanner input = new Scanner(System.in); System.out.print("Enter Student 1 Name ===>> "); String name1 = input.nextLine(); System.out.print("Enter Student 1 Age ===>> "); int age1 = input.nextInt(); System.out.print("Enter Student 2 Age ===>> "); int age2 = input.nextInt(); System.out.print("Enter Student 2 Name ===>> "); String name2 = input.nextLine(); System.out.println("\n\n"); System.out.println("Student 1 Name: " + name1); System.out.println("Student 1 Age: " + age1); System.out.println("Student 2 Name: " + name2); System.out.println("Student 2 Age: " + age2);
Not all input appears to be entered properly; the "name" input for Student 2 is not displayed.
What happens if the number being displayed has more than 5 digits? ------------------------- DecimalFormat output = new DecimalFormat("00000");
Output formatting is ignored; it'll display the number disregarding the DecimalFormat condition.
Look at programs Java0605.java and Java0606.java. Why does the execution of the former produce different results each time while the execution of the latter produces the same results each time? ------------------------ *Java0605* Random rand = new Random(); System.out.println(rand.nextInt()); --------------------------- *Java0606* Random rand = new Random(12345); System.out.println(rand.nextInt());
Output is different with the default constructor. With a parameter constructor the seed value makes each output sequence the same. Aka, the second uses a seed (12345) whereas the first doesn't.
List 2 Math class data members.
PI and E (aka attributes)
Refer to program Java0604.java. What action is performed by the getCombined method?
Returns the sum of the checking and savings account balances.
Look at program Java0632.java. How does this program cure the problem of program Java0630.java? -------------------------------------------------------- Scanner numberInput = new Scanner(System.in); Scanner textInput = new Scanner(System.in); System.out.print("Enter number 1 ===>> "); int number1 = numberInput.nextInt(); System.out.print("Enter name 1 ===>> "); String name1 = textInput.nextLine(); System.out.print("Enter number 2 ===>> "); int number2 = numberInput.nextInt(); System.out.print("Enter name 2 ===>> "); String name2 = textInput.nextLine(); System.out.println("\n\n"); System.out.println("Number 1: " + number1); System.out.println("Name 1 : " + name1); System.out.println("Number 2: " + number2); System.out.println("Name 2 : " + name2);
Separate Scanner objects are created for text and numerical input.
Rewrite System.out.println(rand.nextInt(100)+1); so that it uses Math.random.
System.out.println(Math.random()*100)+1);
Rewrite System.out.println(rand.nextInt(101)+0); so that it uses Math.random.
System.out.println(Math.random()*101)+0);
Rewrite System.out.println(rand.nextInt(41)+20); so that it uses Math.random.
System.out.println(Math.random()*41)+20);
Write the desired range of numbers using the Random class: Range[1,100]
System.out.println(rand.nextInt(100)+1);
Write the desired range of numbers using the Random class: Range[0,100]
System.out.println(rand.nextInt(101)+0);
Write the desired range of numbers using the Random class: Range[20,60]
System.out.println(rand.nextInt(41)+20);
Write the desired range of numbers using the Random class: Range[100,999]
System.out.println(rand.nextInt(900)+100);
What is wrong with program Java0601.java? ------------------------------------------------------- Bank.checkingDeposit(1000.0); Bank.savingsDeposit(5000.0); System.out.println("Checking balance: " + Bank.getChecking()); System.out.println("Savings balance: " + Bank.getSavings()); System.out.println();
The bank class does not allow access in the same manner as the Math class.
Look at program Java0631.java. How does this program cure the program of program Java0630.java? ------------------------------------------------------- Scanner input = new Scanner(System.in); System.out.print("Enter Student 1 Name ===>> "); String name1 = input.nextLine(); System.out.print("Enter Student 1 Age ===>> "); int age1 = input.nextInt(); System.out.print("Enter Student 2 Age ===>> "); int age2 = input.nextInt(); String dummy = input.nextLine(); System.out.print("Enter Student 2 Name ===>> "); String name2 = input.nextLine(); System.out.println("\n\n"); System.out.println("Student 1 Name: " + name1); System.out.println("Student 1 Age: " + age1); System.out.println("Student 2 Name: " + name2); System.out.println("Student 2 Age: " + age2);
The dummy variable absorbs the return character to allow the next input to work correctly.
Refer to program Java0625.java. What would happen if the 2 800s are changed to 400s? --------------------------- Random rndInt = new Random(12345); for (int k = 1; k <= 1000; k++) { int x1 = rndInt.nextInt(800); int y1 = rndInt.nextInt(600); int x2 = rndInt.nextInt(800); int y2 = rndInt.nextInt(600); g.drawLine(x1,y1,x2,y2); }
The lines become shorter and draw on the left side of the screen.
Refer to program Java0625.java. What would happen if the 2 600s are changed to 300s? --------------------------- Random rndInt = new Random(12345); for (int k = 1; k <= 1000; k++) { int x1 = rndInt.nextInt(800); int y1 = rndInt.nextInt(600); int x2 = rndInt.nextInt(800); int y2 = rndInt.nextInt(600); g.drawLine(x1,y1,x2,y2); }
The lines become shorter and draw on the top half of the screen.
In the statement: import java.util.Random; what is the class and what is the package?
The package is util and the the class is Random.
What range of numbers can be displayed with System.out.println(rand.nextInt(100));
[0,99]
What range of numbers can be displayed with System.out.println(rand.nextInt(50)+1);
[1,50]
What range of numbers can be displayed with System.out.println(rand.nextInt(90) + 10);
[10,99]
What range of numbers can be displayed with System.out.println(rand.nextInt(120)+60);
[60,179]
Methods of the Math class are called what type of methods?
class methods
Refer to Java0603.java. What Bank class method makes the checking and savings accounts start at $0.00 when a new Bank object is created?
constructor
Refer to Java0603.java. Print 1 line of Java code that will make a $250,000 deposit in your checking account.
damond.checkingDeposit(250000);
Refer to Java0603.java. Print 1 line of Java code that will make a $500,000 withdrawl from your savings account.
damond.savingsWithdrawl(500000);
What is the difference between double and Double?
double is a simple data type. Double is a class.
What statement must be at the top of your program in order for you to use the DecimalFormat class?
import java.text.DecimalFormat
What is the difference between int and Integer?
int is a simple data type. Integer is a class.
What methods will convert Integer and Double objects back to simple data types?
intValue() as in intObject1.intValue(); and doubleValue() as in doubleObject1.doubleValue();
At the top of this hierarchy is the ____ package.
java
The most important package is the ______ package.
java.lang
Objects contain data and what else?
methods
Speaking algebraically if n is the integer parameter of the nextInt method, what is the largest number you can get in return?
n-1 e.g. rand.nextInt(100); largest number returned is 100-1=99
What Scanner method is used to enter a double?
nextDouble(); As in, double money = input.nextDouble();
What method is used to produce [random] real numbers between 0 and 1?
nextDouble(); As in, rand.nextDouble();
What Scanner method is used to enter an int?
nextInt(); As in, int years = input.nextInt();
What Scanner method is used to enter a String?
nextLine(); As in, String name = input.nextLine();
Methods with multiple capabilities are called _________.
overloaded
In addition to the Random constructor, what other method is used to set the seed for computing random numbers?
setSeed();