In: Computer Science
1. Write a Java program that prompts the user to enter three
integer numbers. Calculate and print the average of the numbers.
2. Write a Java program that uses a for loop to print the odd
numbers from 1 to 20. Print one number per line in the command line
window.
3. Write a program which asks the user to input the size of potatoe
fries she would like to purchase, and based on the size, it will
tell her a price. The prices are as follows:
1
Small
$ 5
2
Medium
$ 10
3
Large
$ 15
The user should type in the number 1, 2 or 3 in order to select the
size. Ensure that you instruct the user to do so in your text input
dialog box.
4. The average age to obtain a driver’s licence is 18. Write a Java
program where the user enters his / her age. If the age is greater
than and equal to 18 the program must display using JOptionPane the
following message, “you are old enough to apply for a driver’s
licence”, else display the following message, “you are not old
enough to apply for a driver’s licence”.
Average.java
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
// Scanner class object to get the
input from user
Scanner sc=new
Scanner(System.in);
System.out.println("Enter three
integer numbers:");
// get three integer numbers from
user
int num1=sc.nextInt();
int num2=sc.nextInt();
int num3=sc.nextInt();
// calculate average
double
average=(num1+num2+num3)/3;
// print average of three
numbers
System.out.printf("Average of three
numbers is: %.2f",average);
sc.close(); // close Scanner
object
}
}
Output
*************************************************************
*************************************************************
OddNumbers.java
public class OddNumbers {
public static void main(String[] args) {
System.out.println("Odd numbers
from 1 to 20 are:");
for(int i=1;i<=20;i++)
// check number
is odd or not
if(i%2!=0)
// print the number
System.out.println(i);
}
}
Output
*************************************************************
*************************************************************
PotatoeFries.java
import javax.swing.JOptionPane;
public class PotatoeFries {
public static void main(String[] args) {
// accept input from input dialog
box
int
size=Integer.parseInt(JOptionPane.showInputDialog("Enter the size
of potatoe fries like to purchase:"));
if(size==1) {
System.out.println(size);
System.out.println("Small");
System.out.println("$5");
}else
if(size==2)
{
System.out.println(size);
System.out.println("Medium");
System.out.println("$10");
}else
if(size==3) {
System.out.println(size);
System.out.println("Large");
System.out.println("$15");
}else
System.out.println("Wrong
input...!!!");
}
}
Output
*************************************************************
*************************************************************
DriverLicence.java
import java.util.Scanner;
import javax.swing.JOptionPane;
public class DriverLicence {
public static void main(String[] args) {
// Scanner class object to get the
input from user
Scanner sc=new
Scanner(System.in);
System.out.print("Enter his/her
age: ");
int age=sc.nextInt(); // get age
from user
// check age is greater than 18 or
not
if(age>=18)
// show message
using JOptionPane message box
JOptionPane.showMessageDialog(null, "You are old enough to apply
for a driver’s licence");
else
JOptionPane.showMessageDialog(null, "You are not old enough to
apply for a driver’s licence");
sc.close(); // close scanner
object
}
}
Output