Question 1.A. Write a java program that uses a Scanner object to read in user in
ID: 3590123 • Letter: Q
Question
Question 1.A. Write a java program that uses a Scanner object to read in user input. The program asks the user for number between 1 and 100. If the number is between 1 and 50 it prints You are in the first half and if the number is greater than 50 it prints You are in the second half.
Sample outputs:
//Prints the first half
Input a number between 1 and 100: 25
You are in the first half.
//Prints the second half
Input a number between 1 and 100: 55
You are in the second half.
Provide:
Printout of Properly Formatted Source Code
Example output/test cases (2 test cases different than above that show both cases).
Question 1.B.
Write a program that uses a Scanner object to read in user input. The program will determine if a user gets a discount for a bus ticket. First ask the user to enter the regular price of a bus ticket. Then ask the user if they are a student (note, the Scanner class has a nextBoolean method to read in boolean values). If they are a student, they get a 15% discount on the price of the ticket. Print the original price, what the discount is (if any) and the new price of the ticket. You need to control for the decimal point to two decimal places. You can control the output of the decimal place by using the printf statement (see the end of this assignment for a description).
Sample outputs:
//student is true
//student is false
Provide:
Printout of Properly Formatted Source Code
Example output/test cases (2 test cases different than above that show both cases).
Make sure it is done in Java format.
//Prints the first half
Input a number between 1 and 100: 25
You are in the first half.
//Prints the second half
Input a number between 1 and 100: 55
You are in the second half.
r-jGRASP exec: java A1_BusTicket Enter the price of the bus ticket: 12 Are you a student (true or false)? true The price of the ticket is $12.00 The discount for the ticket is $1.80 The total cost is $10.20 )GRASP : operation complete.Explanation / Answer
Question 1.A
PrintInfoTest.java
import java.util.Scanner;
public class PrintInfoTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input a number between 1 and 100: ");
int n = scan.nextInt();
if(n > 50) {
System.out.println("You are in the second half.");
} else {
System.out.println("You are in the first half.");
}
}
}
Output:
Input a number between 1 and 100:
55
You are in the second half.
Question 1.B
import java.util.Scanner;
public class TicketPriceTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the price of the bus ticket: ");
double price = scan.nextDouble();
System.out.println("Are you a studnet (true or false): ");
boolean flag = scan.nextBoolean();
double discount = 0;
System.out.println("The price of the ticket is $"+price);
if(flag) {
discount = (price * 15)/100;
System.out.printf("The discount for the ticket is $%.2f ",discount);
} else {
System.out.println("There is no discount for the ticket");
}
double totalPrie = price - discount;
System.out.printf("The total cost is $%.2f ",totalPrie);
}
}
Output:
Enter the price of the bus ticket:
12
Are you a studnet (true or false):
true
The price of the ticket is $12.0
The discount for the ticket is $1.80
The total cost is $10.20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.