Question

In: Computer Science

1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...

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”.

Solutions

Expert Solution

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


Related Solutions

Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT