AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated for you. It is a little misleading because we will NOT add code where it says /* ADD YOUR CODE HERE */. We could do that but we won t. Delete everything in green. Change the code so that it matches the code shown. Save the file onto the desktop (or flash drive). Click the compile tab. Fix any errors. Recompile. Click Run and you should see Hello World displayed in the Interactions panel at the bottom.
public class First { public static void main(string[] args) { System.out.println( "Hello World" ); System.out.println is a magical statement that causes something to be displayed in the Interactions panel. "Hello World" is called a string and System.out.println displays it as written. Create a new class file (name the class Second ) and enter the following code. Compile and run the program. Notice that: The plus sign is used to join the string literal and the value of the variable. The result is what System.out.println displays. System.out.println displays the value of a variable only if the variable is not enclosed by quotes. Create another class file and name the class Third. Enter this code. Compile and run it. A discussion of the code follows on the next page.
import java.util.scanner; public class Third { public static void main(string[] args) { Scanner kb = new Scanner( System.in ); A System.out.println( "Enter an integer:" ); int x = kb.nextint(); B System.out.println( "You entered a " + x ); A) This statement declares a variable, kb, which is of type Scanner. The expression on the right side of the assignment operator creates a Scanner object. The name of the variable can change but keep everything else the same. B) The expression to the right of the assignment operator calls the nextint method which retrieves the first integer entered by the user. This value is then assigned to the variable. Create another file and enter this code. Compile and run the program. Enter 7.8 for the decimal and 6 for the integer. The following message should be displayed: num1 is 7.8, num2 is 0 Please ask if you do not understand why num2 is equal to zero. Notice: We use the nextdouble method if we expect the user to enter a decimal. Since I changed the name of the Scanner variable to x, then I must use x when calling the nextdouble and nextint methods.
1. Create a new file and name it "Problem." Copy the code below and run it. public class Problem { public static void main( String [] args ) { double x = 7.5; double y = 5.4; Correct The program s answer is answer is... System.out.println( x ); System.out.println( y ); double z = x + y; System.out.println( z ); z = x - y; System.out.println( z ); Notice that sometimes the results contain small errors. Here s why: all data are represented as a series of ones and zeros (binary form). Certain decimal values cannot be represented exactly in binary form and so small floating point errors can appear. We will not concern ourselves with these errors but you should be aware that they can occur. 2. This program will implement the formula for calculating the area of a trapezoid: ( base ) * 1 base area height 2 2 Create a new file. In the file, use the Scanner class to get the height and base measurements. Then calculate and display the correct area. The variables representing the height and bases should be ints (this is an arbitrary decision by me) but the area should be a double. For example, if the height is 5 units long and the bases are 3 and 2, then your program should display the correct area: 12.5 square units. 3. This program in which the user enters the number of widgets ordered and the unit price. The program calculates and displays: the cost of the widgets, the 6% tax on the sale, and the total cost. Don't worry about rounding off the results to two places. Yes, you should create a new file. You should use an int for the number of widgets because that is the logical data type.
4. This program should convert degrees Fahrenheit to degrees Celsius. The formula is: 5 C F 32 9 Yes, you create a new file. Fahrenheit should be an integer value but the result is a decimal. 5. Write a program that runs like this - the user enters a number, let s say 10, and the program displays: Half of 10 is 5 Half of 5 is 2.5 Half of 2.5 is 1.25 Half of 1.25 is 0.625 so on. If the user enters a different number then it display half of that, then half of the half, and 6. Write a program where the user enters a positive integer that represents a certain number of hours. The program then displays a message that displays the number in terms of days and hours. For example: if the user enters 51, then the program displays: 51 hours = 2 days and 3 hours If the user enters 18, the program displays: 18 hours = 0 days and 18 hours. You will need to use the mod operator. 7. One flower costs $7 but a dozen flowers cost only $70. Write a program where the user enters the number of flowers and the program displays the cost. For example, if the user enters 27 then the program displays: 27 flowers will cost $161 If the user enters 11 then the program displays: 11 flowers will cost $77