CS110 Java Programming Assignment #2

Instructor: Trish Cornez




  Instructions - Complete the following before the start of class:
  1. Code, debug, comment, and print the source code for each program. All programs must contain your name, date, a program message, and code comments. Points will be deducted on programs that are not well coded and commented and pages that are unstapled.
  2. Execute each program with the input supplied. The last program should execute just once.
  3. Print all executions.






PROGRAM 1:
Write a program that prompts the user for their height in inches. Compute and display their height in feet and inches.

Use your program to verify the following :

Execution 1:
        Enter your height in inches: 65
        You are 5 feet 5 inches tall.

Execution 2:
        Enter your height in inches: 71
        You are 5 feet 11 inches tall.

Execution 3:
        Enter your height in inches: 72
        You are 6 feet 0 inches tall.






PROGRAM 2:
Write a program that prompts the user to input a 4 digit integer - Assume the input will always be exactly 4 digits. Output the integer with the digits in reverse. Write your program so that it requires the use of only two variables total.

Use your program to verify the following :

Execution 1:
        Input: 6578
        Output: 8765

Execution 2:
        Input: 1234
        Output: 4321

Execution 3:
        Input: 1005
        Output: 5001






PROGRAM 3:
The running time for a movie is measured in hours and minutes. Assume that a user plans to watch two movies back-to-back. Prompt them for the running time of each movie and compute the total watching time in hours and minutes. Use the % operator to ensure minutes do not exceed 59.

Use your program to verify the following :

Execution 1:
Movie #1
        Enter the movie 1 hours: 2
        Enter the movie 1 minutes: 40
Movie #2
        Enter the movie 2 hours: 1
        Enter the movie 2 minutes: 20
The total watching time is 4 hours and 0 minutes

Execution 1:
Movie #1
        Enter the movie 1 hours: 1
        Enter the movie 1 minutes: 44
Movie #2
        Enter the movie 2 hours: 1
        Enter the movie 2 minutes: 20
The total watching time is 3 hours and 4 minutes

Execution 3:
Movie #1
        Enter the movie 1 hours: 3
        Enter the movie 1 minutes: 50
Movie #2
        Enter the movie 2 hours: 1
        Enter the movie 2 minutes: 50
The total watching time is 5 hours and 40 minutes






PROGRAM 4:
Angles are often measured in degrees, minutes, and seconds. There are 360 degrees in a circle, 60 minutes in one degree, and 60 seconds in one minute. Write a program that reads two angular measurements given in degrees, minutes, and seconds, and then calculates and prints their sum. Constants are required for this program.

Use your program to verify the following :

Execution 1:
Measurement #1
        Enter the degrees: 74
        Enter the minutes: 29
        Enter the seconds: 13

Measurement #2
        Enter the degrees: 105
        Enter the minutes: 8
        Enter the seconds: 16
The sum of measurement #1 and measurement #2 is 179 degrees, 37 minutes, and 29 seconds

Execution 2:
Measurement #1
        Enter the degrees: 7
        Enter the minutes: 14
        Enter the seconds: 55

Measurement #2
        Enter the degrees: 5
        Enter the minutes: 24
        Enter the seconds: 55
The sum of measurement #1 and measurement #2 is 12 degrees, 39 minutes, and 50 seconds

Execution 3:
Measurement #1
        Enter the degrees: 1
        Enter the minutes: 17
        Enter the seconds: 48

Measurement #2
        Enter the degrees: 237
        Enter the minutes: 42
        Enter the seconds: 12
The sum of measurement #1 and measurement #2 is 239 degrees, 0 minutes, and 0 seconds




PROGRAM 5:


The shipping clerk at the Redlands Handcrafted Bike Shop is faced with the following problem:

Write an interactive Java program that reads the number of handcrafted bikes to be shipped and prints the number of large, medium, and small containers needed to send the shipment in the minimum number of containers and with the minimum amount of wasted space.
TIP: Use contants for the number of bikes each size of container can hold.
Execute this program three times and use the following numbers for input: .

Here are three example executions:


How many handcrafted bikes will be shipped? 26

You will need 2 large containter(s), 1 medium container(s), and 1 small container(s)




How many handcrafted bikes will be shipped? 4

You will need 0 large containter(s), 0 medium container(s), and 4 small container(s)




How many handcrafted bikes will be shipped? 42

You will need 4 large containter(s), 0 medium container(s), and 2 small container(s)





PROGRAM 6:


Write a program that performs long multiplication.
  1. Prompt the user for a three digit integer x
  2. Prompt the user for a two integer y
  3. Compute x multiplied by y using a similar display as follows:
  4. Use System.out.printf to format for right alignment.
    TIP:
    				int a = 123;
    				int b = 13;
    				int c = 369;
    				int d = 1230;
    				System.out.printf("%8d\n", a);
    				System.out.printf("%8d\n", b);
    				System.out.println("  ______");
    				System.out.printf("%8d\n", c);
    				System.out.printf("%8d\n", d);
    			




PROGRAM 7:


Write a program that prompts the user for their first, middle, and last names, in uppercase. Display number of letters in their name and their initials in lowercase.

Example: Input
SUE
BORT
THOMAS

Output: You have 13 letters in your name and your initials are sbt



PROGRAM 8:


Assume that a robot is used to tile a floor with alternating black and white tiles. Write a program that yields the color (0 for black, 1 for white), given the row and column number.

Sample execution 1:
    input row: 1
    input column: 2
    Result: The color for this tile is 1.



Sample execution 2:
    input row: 3
    input column: 5
    Result: The color for this tile is 0.





PROGRAM 9:


Part 1: Research the topic of primitive data types in Java. Briefly describe the eight primitive data types supported by the Java programming language.
Part 2: Execute the code below and briefly explain the unusual output.
		public static void main(String[] args) {
			int x;
			x = (int) Math.pow(2, 31);
			System.out.println("The largest value that can be stored in x is " + x);
			x = x + 1;
			System.out.println("x + 1 = " + x);
		}