0% found this document useful (0 votes)
17 views2 pages

ArithmeticVarsIPOpart2 Nov2023

Uploaded by

holycowman01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

ArithmeticVarsIPOpart2 Nov2023

Uploaded by

holycowman01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming Exercises – IPO, Variables, Arithmetic

Write each program using appropriate variables (number and type) and input methods.

1. Hollow Square Calculator


Write a program that asks the user for a radius in pixels, and then calculates and outputs the area r
of the shaded portion (as shown), rounded to the nearest square pixel.

Hollow Square Calculator!!! Example


output… 2r
Enter the radius of a circle (in pixels): 200
Your hollow square (the shaded region around the circle) has an area of 34336 pixels squared.

2. Create a Temperature program that will convert last week’s daily temperatures in Toronto,
input by the user in Celsius, to Fahrenheit, and calculate the average weekly temperature in
both units. Display the results (all daily temperatures in both units, and both averages) in a
nicely formatted table (see below). Declare variables (with appropriate names and data types)
to store the temperatures (Celsius and Fahrenheit) for each day of the week, and the weekly
average (in both Celsius and Fahrenheit). Display the weekly averages rounded to 2 decimals.

Sample Output:
Toronto Temperatures (Week of Oct. 29)
Sunday Monday Tuesday Wednesday Thursday Friday Saturday AVERAGE
Celsius 9 10 9 7 14 12 9 10.00
Fahrenheit 48.2 50 48.2 44.6 57.2 53.6 48.2 50.00

3. Create a MakeLabels program that consists of four (4) variables that are used to store the user’s name, student
number, grade, and date of birth, and outputs the information to the screen in the following format:

Pay special attention to format and spacing of the output. You can use the appropriate escape sequences and
escape characters, or use field widths with your print / println statements.
4. Days Alive:
Write a program that calculates the number of days you have been alive and the number of hours of your life
that you have spent sleeping. Assume that you sleep 8 hours each night. To simplify the problem, assume there
are 30 days in each month and 365 days in each year. The output should look something like this:

Analyzing & Interpreting Java code


It is very important that you develop the ability to read, understand, and interpret code and resulting output without
the aid of a computer and compiler.

1. What would be the exact output of the following Java code?

int x;
x = 3;
int y = x;
x = 5;
y = y + x;
System.out.println(x);
System.out.println(y);

2. What would be the exact output of the following Java fragment code?

System.out.println("Hello" + 42);
System.out.println(1 + "Hello" + 2);
System.out.println("Hello" + 4 + 2);
System.out.println(1 + 2 + "Hello");
System.out.println("Hello"+9*3);
System.out.println("1"+1);

You might also like