Name: _________________________________ Java Programming – Midterm Review Sheet Mrs. C. Furman – Fall 2008 Change binary to decimal, decimal to binary and decimal to hexadecimal Decimal Binary Hexadecimal 59 0011 1011 3B 204 1100 1100 CC 108 0110 1100 6C 23 0001 0111 17 What makes a proper identifier?? contains numbers, letters, and underscores ( _ ). Must start with a letter. Can’t be a keyword. What are the 4 primitive data types? int, double, boolean, char Declare and create variables for the following: o your age int age; o length of a square double squareLength; o temperature outside int outsideTemp; o body temperature double bodyTemp; o answer to a true / false question boolean answer; o your middle initial char initial; o your house number int houseNum; Compute the following: o 2 + 3 * 12 = 38 o (2 + 3) * 12 = 60 o 10 / 3 = 3 o 10 / 3.0 = 3.3333333 o 10 % 3 = 1 o 7 + 3 % 2 * 7 = 14 Declare and create a Scanner for input. Scanner input = new Scanner (System.in); Data Conversions: What are the 2 types of conversions? Widening and Narrowing What are the 3 ways that we convert data from one type to another? assignment promotion, arithmetic promotion, and casting What type can not be converted? boolean x = 5 / 2; //assigns 2 to x. How can we change it so that we get 2.5 assigned? x = 5.0 / 2; x = 5.0 / 2.0; x = 5 / 2.0; x = (double) 5 / 2; x = (double) 5/ (double) 2; x = 5 / (double)2; What’s the difference between an applet and an application? applet has no main, usually meant to run on the web. Application is stand – alone. Has a main method. Given Graphics g, how do we draw a rectangle that is 20 by 30 and has an upper left coordinate of 10, 10? g.drawRect (10, 10, 20, 30); Given Graphics g, how do we set the color of our brush to blue? g.setColor (Color.BLUE); How do you create new colors? Color myNewColorName = new Color (3, 45, 65); How do you make a triangle, with endpoints at (20, 20), (55, 20), and (35, 40)? Polygon triangle = new Polygon (); triangle.addPoint (20, 20); triangle.addPoint (55, 20); triangle.addPoint (35, 40); g.drawPoly (triangle); What would the output be for: System.out.println (“The answer is “ + 24 + 3); The answer is 243 System.out.println (“The answer is “ + (24 + 3)); The answer is 27 int x = 5; System.out.println (“The answer is “ + x); The answer is 5 System.out.println (“Mary had a little lamb, \tlittle lamb, \tlittle “ + “lamb,\n\”Mary\” had a little lamb \n\t\tIt\’s fleece was white as snow.”); Mary had a little lamb, little lamb, little lamb, “Mary” had a little lamb It’s fleece was white as snow. We are trying to organize transportation for a field trip. We know that the bus capacity is 35 people. Write statements which will tell us how many busses we will need given the number of people attending the trip. int totalPeople; final int BUSCAPACITY = 35; int numBusses; numBusses = Math.ceil ( (double)totalPeople / BUSCAPACITY); String: Declare and create a String called name, which will hold “Furman”. String name = new String (“Furman”); String name = “Furman”; Declare and create a String called newName, which will insert the “ry” at index 2. String newName = name.substring (0, 3) + “ry” + name.substring (3); //newName = F u r r y m a n 01234567 Write a series of statements which will assign i to the index of the word “man” in newName. First declare i. int i = newName.indexOf(“man”); //i = 5 Write a series of statements which will assign len to the length of newName. You must first declare len. int len = newName.length(); What is assigned to len? 8 Write a series of statements which will declare and create a new String called first and assign it the substring of newName that is equivalent to “furry”. String first = newName.substring(0, 5); Write a statement which will output the character that is found at the 3 rd position of newName. System.out.println (newName.charAt(2));//prints r Write a series of statements which will take a name from the user and reassign it to name. Please declare and create a Scanner for input, and prompt the user to enter their name. Scanner keyboard = new Scanner (System.in); System.out.println (“Enter a name”); name = keyboard.nextLine(); OR name = keyboard.next(); Translate the formula using Math class. area side 2 perimeter 2l 2w circumference 2 r slope y2 y1 x2 x1 dist ( x2 x1 ) 2 y2 y1 2 dist x1 x 2 area = Math.pow(side, 2); perimeter = 2*l + 2*w; circumference = 2 * Math.PI * r; slope = (y2 – y1) / (x2 – x1); dist = Math.sqrt(Math.pow ((x2 – x1), 2) + Math.pow ((y2 – y1), 2)); dist = Math.abs(x1 – x2); A packet of flyers contains 30, if each person needs a flyer and there are 72 people, write a statement which will assign totalneeded to the number of flyers we should get. final int packet = 30; int people = 72; int totalNeeded = Math.ceil(people / (double)packet);