If you want to use an existing class from the Java Class Library in your program, what
keyword should you use?
import
1/73
Term | Definition |
---|---|
If you want to use an existing class from the Java Class Library in your program, what keyword should you use? | import |
A constructor has the same name as the class name. | true |
A given class can have more than one constructor. | true |
What is the keyword used to instantiate an object in Java? | new |
In a given class named Quiz, there can be only one method with the name Quiz. | false |
A static method is | one that does not need to be instantiated as an obj to be used |
In the Quiz class, the foo method has the following API: public static double foo( float f ) What can you say about foo? | foo is a float num being represented as a double, the data can be messed with by stuff outide the class, and it doesn't need to be instantiated? |
In the Quiz class, the foo method has the following API: public static void foo( ) How would you call that method? | varName.foo(); |
In the Quiz class, the foo method has the following API: public double foo( int i, String s, char c ) How many arguments does foo take? | 3 |
In the Quiz class, the foo method has the following API: public double foo( int i, String s, char c ) What is the return type of method foo? | a double |
String is a primitive data type in Java | No |
Which one of the following is not an existing wrapper class? # Integer # Char # Float # Double | Char |
What is the proper way of accessing the constant E of the Math class? | Math.E; |
What is the output of this code sequence? String s = new String( "HI" ); System.out.println( s ); | HI |
What is the output of this code sequence? String s = "A" + "BC" + "DEF" + "GHIJ"; System.out.println( s ); | ABCDEFGHIJ |
What is the output of this code sequence? String s = "Hello"; s = s.toLowerCase( ); System.out.println( s ); | hello |
What is the output of this code sequence? int a = Math.min( 5, 8 ); System.out.println( a ); | 5 |
What is the output of this code sequence? System.out.println( Math.sqrt( 4.0 ) ); | 2.0 |
What is the output of this code sequence? (You will need to actually compile this code and run it in order to have the correct output.) System.out.println( Math.PI ); | 3.14 and then it keeps going for a bit |
What is the output of this code sequence? double f = 5.7; long i = Math.round( f ); System.out.println( i ); | 6 |
What is the output of this code sequence? System.out.print( Math.round( 3.5 ) ); | 4 |
What is the output of this code sequence? int i = Math.abs(-8 ); System.out.println( i ); | 8 |
What is the output of this code sequence? double d = Math.pow( 2, 3 ); System.out.println( d ); | 8.0 |
This code concatenates the three Strings “Intro ”, “to”, “ Programming” and outputs the resulting String. (Your output should be Intro to Programming String s1 = "Intro "; String s2 = "to"; String s3 = " Programming"; // your code goes here | System.out.println(s1 + s2 + s3); |
This code prints the number of characters in the String “Hello World”. String s = "Hello World"; // your code goes here | System.out.println(s); |
This code prompts the user for a String , then prints the String and the number of characters in it. // your code goes here | System.out.println("Please type in a few words: "); String typedChars = scan.next(); int numChars = typedChars.length(); System.out.println("You typed: " + typedChars); System.out.println("Which is " + numChars + " characters."); |
This code uses only a single line System.out.println . . . statement in order to print “Welcome to Java Illuminated” on one line using the following variables and the String s1 = "Welcome "; String s2 = "to "; String s3 = "Java "; String s4 = "Illuminated"; // your code goes here | System.out.println(s1 + s2 + s3 + s4); |
This code uses exactly four System.out.print statements in order to print “Welcome to Java Illuminated” on the same output line. // your code goes here | System.out.print(s1); System.out.print(s2); System.out.print(s3); System.out.print(s4); |
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result. int max; // your code goes here | max = Math.max(3,5); System.out.println(max); |
This code calculates the square root of 5 and outputs the result. double d = 5.0; // your code goes here | d = Math.sqrt(d); System.out.println(d); |
This code asks the user for two integer values, then calculates the minimum of the two values and prints it. // your code goes here | Scanner scan = new Scanner(System.in); System.out.println("Give me two numbers written 0,0: "); String text = scan.next(); int sep = text.indexOf(','); String fst = text.substring(0,sep); String scd = text.substring(sep + 1); int firstNum = Integer.parseInt(fst); int secNum = Integer.parseInt(scd); int result = Math.min(firstNum, secNum); System.out.println(result); |
This code asks the user for three integer values, then calculates the maximum of the three values and prints it. // your code goes here | Scanner scan = new Scanner(System.in); String text = scan.next(); String fst = text.substring(0, 1); String scd = text.substring(2,3); String trd = text.substring(4,5); int NumOne = Integer.parseInt(fst); int NumTwo = Integer.parseInt(scd); int NumThr = Integer.parseInt(trd); int maxNum1 = Math.max(NumOne, NumTwo); int maxNum = Math.max(maxNum1, NumThr); System.out.println("The largest number is " + maxNum); |
33. This code prompts the user for a single character and prints “true” if the character is a letter and “false” if it is not a letter. // your code goes here | Scanner scan = new Scanner(System.in); System.out.println("Write a character: "); String text = scan.next(); char letter = text.charAt(0); boolean result = Character.isLetter(letter); System.out.println(result); |
This code asks the user for a double, then prints the square of that number using the method of the Math class. // your code goes here | import java.util.Scanner; Scanner scan = new Scanner(System.in); System.out.println("Write a number: "); String text = scan.next(); int num = Integer.parseInt(text); double result = Math.sqrt(num); System.out.println(result); |
Where is the error in this statement? import text.NumberFormat; | needs the java in front -> java.text.NumberFormat; |
Where is the error in this statement? import java.util.DecimalFormat; | .text not .util |
Where is the error in this code sequence? String s = "Hello World"; system.out.println( s ); | cap the first S |
Where is the error in this code sequence? String s = String( "Hello" ); System.out.println( s ); | dont need the String before hello, also the () do nothing |
Where is the error in this code sequence? String s1 = "Hello"; String s2 = "ello"; String s = s1 - s2; | Not at all how fuckin strings work |
Where is the error in this code sequence? short s = Math.round( 3.2 ); System.out.println( s ); | lossy as far the comp. is concerned, float to short no good |
Where is the error in this code sequence? int a = Math.pow( 3, 4 ); System.out.println( a ); | Math.pow makes a double, so this is also lossy |
Where is the error in this code sequence? double pi = Math( PI ); System.out.println( pi ); | should be Math.PI; |
Where is the error in this code sequence? String s = 'H'; System.out.println( "s is " + s ); | string not a char first of all, then '' instead of "" to make is a string |
You coded the following program in the file Test.java: public class Test { public static void main( String [ ] args ) { int a = 6; NumberFormat nf = NumberFormat.getCurrencyInstance( ); } } When you compile, you get the following message: Test.java: 6: error: cannot find symbol NumberFormat nf = NumberFormat.getCurrencyInstance( ); ^ symbol : class NumberFormat location: class Test Test.java: 6: error: cannot find symbol NumberFormat nf = NumberFormat.getCurrencyInstance( ); ^ symbol : variable NumberFormat location: class Test 2 errors | You did not import NumberFormat |
You coded the following on lines 10–12 of class Test.java: String s; int l = s.length( ); System.out.println( "length is " + l ); When you compile, you get the following message: Test.java:11: error: variable s might not have been initialized. int l = s.length( ); ^ 1 error Explain what the problem is and how to fix it | String has no values dum dum, give it legit any value |
You coded the following on lines 10 and 11 of class Test.java: double d = math.sqrt( 6 ); System.out.println( "d = " + d ); When you compile, you get the following message: Test.java: 10: error: cannot find symbol double d = math.sqrt( 6 ); ^ symbol : variable math location: class Test 1 error Explain what the problem is and how to fix it. | caps Math |
You coded the following on lines 10 and 11 of class Test.java: double d = Math.PI( ); System.out.println ( "d = " + d ); // line 11 When you compile, you get the following message: Test.java:10: error: cannot find symbol double d = Math.PI( ); // line 10 ^ symbol : method PI( ) location: class Math 1 error Explain what the problem is and how to fix it. | No need () |
You coded the following on lines 10 and 11 of class Test.java: double d = Math.e; System.out.println( "d = " + d ); // line 11 When you compile, you get the following message: Test.java:10: error: cannot find symbol double d = Math.e; // line 10 ^ symbol : variable e location: class Math 1 error | caps E |
You imported the DecimalFormat class and coded the following in the class Test.java: double grade = .895; DecimalFormat percent = new DecimalFormat( "#.0%" ); System.out.println( "Your grade is " + grade ); The code compiles properly and runs, but the result is not what you expected. You expect this output: Your grade is 89.5% But instead, the output is Your grade is 0.895 Explain what the problem is and how to fix it. | You never formatted the num into the decimal format String result = percent.format(grade); |
Write a program that prompts the user for a domain name. Your program should then concatenate that name with www. and .com in order to form an Internet domain name and output the result. For instance, if the name entered by the user is yahoo.com | import java.util.Scanner; public class App { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Please enter a domain name: "); String text = scan.next(); System.out.println("www." + text + ".com"); } } |
Write a program that reads a word from the keyboard. Your program should output the word in uppercase letters only, output that word in lowercase letters only, and then, at the end, output the original word. | import java.util.Scanner; public class App { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Gimmie a word: "); String rawText = scan.next(); String upText = rawText.toUpperCase(); String lowText = rawText.toLowerCase(); System.out.println(upText); System.out.println(lowText); System.out.println(rawText); } } |
Write a program that generates two random numbers between 0 and 100 and prints the smaller of the two numbers. | Don't know method for random |
54. Write a program that takes a double as an input, then computes and outputs the cube of that number using the pow method of the Math class. | import java.util.Scanner; public class App { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Gimmie a number, I will square it: "); double num = scan.next(); double result = Math.pow(num, 2); System.out.print(result); } } |
Write a program that reads a file name from the keyboard. You should expect that the file name has one . (dot) character in it, separating the file name from the file extension. Retrieve the file extension and output it. For instance, if the user inputs index.html, you should output html; if the user inputs MyClass.java, you should output java . | import java.util.Scanner; public class App { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Please enter a file name: "); String text = scan.next(); int dotFind = text.indexOf('.'); String result = text.substring(dotFind + 1); System.out.print(result); } } |
Write a program that reads a full name (first name and last name) from the keyboard as a single line; you should expect the first name and the last name to be separated by a space. Retrieve and output the first and last names. | import java.util.Scanner; public class App { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("First and Last name please: "); String text = scan.nextLine(); int spaceFind = text.indexOf(" "); String first = text.substring(0, spaceFind); String last = text.substring(spaceFind + 1); System.out.println(first); System.out.println(last); } } |
Write a program that reads three integer values from the keyboard representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result with a dollar notation. | import java.util.Scanner; import java.text.DecimalFormat; public class App{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); DecimalFormat money = new DecimalFormat("$#,###.00"); System.out.print("Input quarters, dimes, and nickles written 0,0,0: "); String nums = scan.next(); // get the numbers seperated String qLett = nums.substring(0,1); String dLett = nums.substring(2,3); String nLett = nums.substring(4); //get the strings to be actual ints int qNum = Integer.parseInt(qLett); int dNum = Integer.parseInt(dLett); int nNum = Integer.parseInt(nLett); //become pennies them up double qValue = qNum * .25; double dValue = dNum * .10; double nValue = nNum * .05; //add double result = qValue + dValue + nValue; //get them into currency notation String last = money.format(result); System.out.print(last); } } |
Write a program that reads from the keyboard the radius of a circle. Calculate and output the area and the circumference of that circle. You can use the following formulas: area = π * r2 circumference = 2 * π * r | import java.util.Scanner; public class App{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Tell me the radius of your circle: "); String text = scan.next(); int rad = Integer.valueOf(text); double area = Math.PI * Math.pow(rad, 2); double circum = 2 * Math.PI * rad; System.out.println("Area: " + area); System.out.print("Circumference: " + circum); } } |
Write a program that generates five random integers between 60 and 100 and calculates the smallest of the five numbers. | import java.util.Random; public class App{ public static void main(String[] args){ Random random = new Random(); int first = random.nextInt(100-60+1)+60; int second = random.nextInt(100-60+1)+60; int third = random.nextInt(100-60+1)+60; int fourth = random.nextInt(100-60+1)+60; int fifth = random.nextInt(100-60+1)+60; int oneTwo = Math.min(first, second); int andThree = Math.min(oneTwo, third); int andFour = Math.min(andThree, fourth); int andFive = Math.min(andFour, fifth); System.out.print(andFive); } } |
Write a program that generates three random integers between 0 and 50, calculates the average, and prints the result to one decimal place. | import java.util.Random; public class App{ public static void main(String[] args){ Random random = new Random(); int first = random.nextInt(100-60+1)+60; int second = random.nextInt(100-60+1)+60; int third = random.nextInt(100-60+1)+60; System.out.println(first); System.out.println(second); System.out.println(third + "\n"); double maths = (first + second + third) / 3.0; System.out.print(maths); } } |
Write a program that reads two integers from the keyboard: one representing the number of shots taken by a basketball player, the other representing the number of shots made by the same player. Calculate the shooting percentage and output it with the percent notation. | |
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients a, b, and c of a quadratic equation. Solve the equation using the following formulas: x1 = (-b + square root (b^2 - 4ac))/(2a) x2 = (-b - square root (b^2 - 4ac))/(2a) Run your program on the following sample values: a = 1.0, b= 3.0, c= 2.0 a = 0.5, b= 0.5, c= 0.125 a = 1.0, b= 3.0, c = 10.0 Discuss the results for each program run, in particular what happens in the last case. | |
Write a program that reads from the keyboard the (x,y)coordinates for two points in the plane. You can assume that all numbers are integers. Using the Point class from the Java Class Library (you may need to look it up on the Web), instantiate two Point objs with your input data, then output the data for both Point objects | |
Write a program that reads an email address. Assuming that the email address contains one at sign (@), extract and print the username and the domain name of the email address. | |
Write a program that reads a telephone number from the keyboard as a String of 10 digits. You should output that same telephone number formatted as (nnn) nnn-nnnn. | |
Write a program that reads a sentence from the keyboard. The sentence has been encrypted so that the message consists of only the first five characters with even-numbered indexes. All other characters should be discarded. Decrypt the sentence and output the result. For example, if the user inputs “Hiejlzl3ow”, your output should be Hello . | |
Write a program that reads a commercial website URL from the keyboard; you should expect that the URL starts with www. and ends with .com. Retrieve the name of the site and output it. For instance, if the user inputs www.yahoo.com, you should output yahoo | |
At this point, we have written and debugged many examples of code. When you compile a Java program with the Java compiler, you get a list of all the errors in your code. Do you like the Java compiler? Do the error messages it displays when your code does not compile help you determine what’s wrong? How? | |
Computers, computer languages, and application programs existed before object-oriented programming. However, OOP has become an industry standard. Discuss the advantages of using OOP compared to using only basic data types in a program | |
Explain and discuss a situation where you would use the method parseInt of the class Integer | |
In addition to the basic data types ( int float char boolean) Java provides many prewritten classes, such as Math NumberFormat DecimalFormat. Why is this an advantage? How does this impact the way a programmer approaches a programming problem in general? | |
The group project aka #73 | |
Write a program that reads two numbers from the keyboard representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * ( 1 + interest rate )year We will assume that the interest rate is an annual rate and is compounded annually | |
Write a program that reads two words representing passwords from the keyboard and outputs the number of characters in the smaller of the two. For example, if the two words are open and sesame , then the output should be 4, the length of the shorter word,open |