Very Basic Java Programs


Java – Celsius to Fahrenheit

 

Write a Java program to convert a Celsius value of 18.9 to Fahrenheit.

Use given code to convert 18.9 degrees Celsius to degrees Fahrenheit. Modify by changing app name and record any errors.

Screen Shot 2013-10-16 at 06.56.43

Lab02_3_code

Screens

LAB02_3_Test4


Calculate Area of a Circle

Write a java program to calculate the area of a circle with a radius of 10.

– Write a java program to calculate the area of a circle with a radius of 10. – Compile and Run your program.

– Declare variables: pi, radius and theArea – Process – calculate the Area using: pi * (radius * radius) formula

– Output – print result of the calculation

LAB02_4_Flowchart

LAB02_4_Code

LAB02_4_Test4


Add 9 and 91

Using the area of a circle program as a template change the code to ADD the following two integers together. The first integer will have a value of 9 and the second 91.

LAB02_5_Flowchart

LAB02_5_Code

LAB02_5_Test1


Calculate Area of a Rectangle

Using the area of a circle program as a template, change the code to calculate the area of a rectangle where the sides of the rectangle are 70 and 10.

LAB02_6_Flowchart

LAB02_6_Code

LAB02_6_Test1


Area of a Rectangle > 40 m2

Develop a program that calculates the area of a rectangle area = length * breadth. If the area is greater than 40 meters print one thing, otherwise print something else. 

– Write a program which calculates the area of a rectangle using formula area = length * breadth. – If the area is greater than 40 meters squared print out “This rectangle is too big, it has a length of xxx and breath of xxxx please reduce one of the values.” – Otherwise printout “this rectangle will fit, the length you entered was xxxx, the breath was xxxxx. The area of the triangle will be xxxxxx”

LAB03_1_Flowchart

LAB03_1_Code1LAB03_1_Code2

LAB03_1_Test1 LAB03_1_Test2


Verify Size of an Area of a Rectangle

Based on lab 3.1, Area < 20 print too small, Area > 20 and Area < 40 print perfect size, Area > 40 and Area < 60 print too big.

– Use arithmetic expressions with constants. – Use nested IF ELSE

LAB03_2_Flowchart

LAB03_2_Code1 LAB03_2_Code2

LAB03_2_Test1 LAB03_2_Test2 LAB03_2_Test3


Program Printing Out Numbers

Create a program to print out numbers from 8 to 80 in steps of 2 

– Use arithmetic expressions with constants. – Use a WHILE loop

LAB03_3_Flowchart_v3

LAB03_3_Code_v3

LAB03_3_Test1


Program Printing Out the Prime Numbers

Create a program to print out the prime numbers between the numbers 1 to 100.

– Generate prime numbers between 1 and 100 – A prime number has no positive divisors other than 1 and itself – 1 is not a prime number

The program starts with number 2 and checks each number 3,4,5, and so forth. Each time it finds a prime it prints the number and increments by 1. When the number hits 100, it stops the process. To determine whether a number is prime, it divides number to check if it gives a reminder. For each number in the loop a boolean isPrimeNumber is equal to true. If the number has no reminder, then the boolean isPrimeNumber is set to false. It prints number if boolean isPrimeNumber = true.

LAB03_4_Flowchart

LAB03_4_Code

LAB03_4_Test1


Program That Reads in 10 Numbers

Create a program that uses the Scanner library to reads in 10 numbers between 10 and 30.

– Create a program that uses the Scanner library to reads in 10 numbers between 10 and 30. – Once all 10 numbers are displayed the average of the 10 numbers is calculated and displayed. – If an invalid number is entered e.g. a number not between 10 and 30 then the user is asked to re-enter a valid number. – The invalid entry is not counted as one of the 10 entries.

LAB03_5_Flowchart

LAB03_5_Code_v1 LAB03_5_Code_v5

LAB03_5_TestA LAB03_5_TestB LAB03_5_TestC LAB03_5_TestD


Program Calculating Area of a Rectangle

Develop a program that calculates the area of a rectangle: area = length * breadth.
– Use the javabook class library InputBox class to ask the user for the length and breath.
– Display the results using the OutputBox class

– Program that input and output data using the Javabook InputBox and OutputBox classes.
– Use arithmetic expressions.

LAB04_1_Flowchart

LAB04_1_Code

LAB04_1_Test1 LAB04_1_Test2 LAB04_1_Test3 LAB04_1_Test4 LAB04_1_Test5 LAB04_1_Test6


Program Converting Celsius to Fahrenheit

– Write a program that converts temperatures from Celsius to Fahrenheit
– Fahrenheit = 1.8 * Celsius + 32 (1.8 and 32 are defined as constants).
– Use the javabook class library InputBox class to ask the user for the Celsius value to be converted.
Display the results using the OutputBox class.

– Implement constants – 1.8 and 32 are defined as constants.
– Write arithmetic expressions with constants.

– Input: user input in Celsius degree.
– Processing: arithmetic expressions with constants.
– Output: display the result using the OutputBox class.

LAB04_2_Flowchart

LAB04_2_Code

LAB04_2_Test1 LAB04_2_Test2 LAB04_2_Test3 LAB04_2_Test4 LAB04_2_Test5 LAB04_2_Test6 LAB04_2_Test7 LAB04_2_Test8


Program Calculating Area of a Circle

– Write a program that calculates the area of a Circle.
– Use the javabook class library InputBox class to ask the user for the radius of the circle.
– Use the built in Java Maths library.
– Display the results using the OutputBox class.

– Area = π r2.
– Use the Maths library to obtain a value for π.
– Calculate the power of r
2 using the pow() function.
– Use the javabook class library InputBox class to ask the user for the radius of the circle.
– Display the results using the OutputBox class.

LAB04_3_Flowchart

LAB04_3_Code

LAB04_3_TestA1 LAB04_3_TestA2 LAB04_3_TestB1 LAB04_3_TestB2


Calculating an Average of Three Numbers

Develop a program that asks the user to input 3 numbers and then returns the average of the 3 numbers. 

– Users input: 3 numbers
– Output: average of the 3 numbers
Use the javabook class library InputBox class to ask the user for each number.
– Display the results using the OutputBox class.

LAB04_4_Flowchart

LAB04_4_Code_v2

LAB04_4_TestA LAB04_4_TestB LAB04_4_TestC


Throwing of a Dice

Develop a program that when run simulates the throwing of a dice.

– Simulation of the throwing of a dice
Display the results using the OutputBox class.

LAB04_5_Flowchart

LAB04_5_Code

LAB04_5_Test1 LAB04_5_Test2

Tagged: