Lab 3
LAB TASK 1
Suppose you want to develop a program that changes a given amount of money into smaller monetary units. The program lets the user enter an amount as a double value representing a total in dollars and cents, and outputs a report listing the monetary equivalent in the maximum number of dollars, quarters, dimes, nickels, and pennies, in this order, to result in the minimum number of coins Here are the steps in developing the program:
1. Prompt the user to enter the amount as a decimal number, such as 11.56.
2. Convert the amount (e.g., 11.56) into cents (1156).
3. Divide the cents by 100 to find the number of dollars. Obtain the remaining cents using the cents remainder 100.
4. Divide the remaining cents by 25 to find the number of quarters. Obtain the remaining cents using the remaining cents remainder 25.
5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining cents using the remaining cents remainder 10.
6. Divide the remaining cents by 5 to find the number of nickels. Obtain the remaining cents using the remaining cents remainder 5.
7. The remaining cents are the pennies.
8. Display the result.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the dollar amount: ");
double input = scanner.nextDouble();
input = input*100;
int dollar = (int) input;
int cents = dollar%100;
dollar = dollar/100;
int quarter = cents/25;
cents = cents%25;
int dimes = cents/10;
cents = cents%10;
int nickels = cents/5;
cents = cents%5;
System.out.println("Dolars: "+dollar);
System.out.println("Quarters: "+quarter);
System.out.println("Dimes: "+dimes);
System.out.println("Nickels: "+nickels);
System.out.println("Pennies: "+cents);
scanner.close();
}
}
LAB TASK 2
N students take K apples and distribute them among each other evenly. The remaining (the undivisible) part remains in the basket. How many apples will each single student get? How many apples will remain in the basket? The program reads the numbers N and K. It should print the two answers for the questions above.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int students = scanner.nextInt();
System.out.print("Enter number of apples: ");
int apples = scanner.nextInt();
int share = apples/students;
int remaining = apples%students;
System.out.printf("Each student will get %d apples",share);
System.out.printf("\nRemaining apples are %d",remaining);
scanner.close();
}
}
LAB TASK 3
A school decided to replace the desks in three classrooms. Each desk sits two students. Given the number of students in each class, print the smallest possible number of desks that can be purchased. The program should read three integers: the number of students in each of the three classes, a, b and c respectively.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students in class A: ");
int classA = scanner.nextInt();
System.out.print("Enter number of students in class B: ");
int classB = scanner.nextInt();
System.out.print("Enter number of students in class C: ");
int classC = scanner.nextInt();
int desksA = classA/2;
int remDesksA = classA%2;
int desksB = classB/2;
int remDesksB = classB%2;
int desksC = classC/2;
int remDesksC = classC%2;
int totalDesks = desksA + desksB + desksC + remDesksC + remDesksB + remDesksA;
System.out.println("The required desks for all students: "+totalDesks);
scanner.close();
}
}
LAB TASK 4
Given the integer N – the number of minutes that is passed since midnight - how many hours and minutes are displayed on the 24h digital clock? The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 59). For example, if N = 150, then 150 minutes have passed since midnight - i.e. now is 2:30 am. So the program should print 2 30.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of minutes passed: ");
int minutes = scanner.nextInt();
int hours = minutes/60;
minutes = minutes%60;
System.out.printf("The time is '%d:%d'",hours,minutes);
scanner.close();
}
}
LAB TASK 5
A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one liter of milk is $0.38, and the profit of each carton of milk is $0.27. Write a program that does the following:
a. Prompts the user to enter the total amount of milk produced in the morning
b. Outputs the number of milk cartons needed to hold milk (Round your answer to the nearest integer.)
c. Outputs the cost of producing milk
d. Outputs the profit for producing milk
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount of milk produced in liters: ");
double milk = scanner.nextDouble();
double cartons = milk/3.78;
int cartonsInt =(int) Math.round(cartons);
double cost = milk*0.38;
double profit = cartons*0.27;
System.out.println("Number of cartons required: "+cartonsInt);
System.out.println("The cost for this much is: "+cost);
System.out.println("The profit is: "+profit);
scanner.close();
}
}
LAB TASK 6
You found an exciting summer job for five weeks. It pays $15.50 per hour. Suppose that the total tax you pay on your summer job income is 14%. After paying the taxes, you spend 10% of your net income to buy new clothes and other accessories for the next school year and 1% to buy school supplies. After buying clothes and school supplies, you use 25% of the remaining money to buy savings bonds. For each dollar you spend to buy savings bonds, your parents spend $0.50 to buy additional savings bonds for you. Write a program that prompts the user to enter the pay rate for an hour and the number of hours you worked each week. The program then outputs the following:
a. Your income before and after taxes from your summer job
b. The money you spend on clothes and other accessories
c. The money you spend on school supplies
d. The money you spend to buy savings bonds
e. The money your parents spend to buy additional savings bonds for you
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double rate = 15.5;
double totaltax = 0.14;
System.out.print("Enter number of hours you worked per week: ");
int hours = scanner.nextInt();
System.out.print("Enter pay rate for an hour: ");
rate = scanner.nextDouble();
double wage = rate*hours*5; //5 are weeks
double wageTotal = wage;
totaltax = wage * totaltax;
wage = wage - totaltax;
double accessories =(wage*0.1);
double supplies =(wage*0.01);
wage = wage - (accessories+supplies);
double bonds = wage*0.25;
wage = wage - bonds;
System.out.println("Income before tax paid: "+wageTotal);
System.out.println("Income after tax paid: "+(wageTotal-totaltax));
System.out.println("Money spend on clothes and accessories: "+accessories);
System.out.println("Money spend on school supplies: "+supplies);
System.out.println("Money spend on saving bonds: "+bonds);
System.out.println("Money soend by parents on bonds: "+bonds/2);
scanner.close();
}
}
LAB TASK 7
A cricket game is to be held in a stadium and there are four seating categories available for the audience. Class A seats cost $20, Class B seats cost $15, Class C seats cost $10, and Class D seats cost $5. You should write a JAVA program that asks how many tickets for each class of seats were sold and finally display the total income generated and income corresponding to ticket sales.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Tickets sold for class A seats: ");
int classA = scanner.nextInt();
System.out.print("Tickets sold for class B seats: ");
int classB = scanner.nextInt();
System.out.print("Tickets sold for class C seats: ");
int classC = scanner.nextInt();
System.out.print("Tickets sold for class D seats: ");
int classD = scanner.nextInt();
int incomeA = classA*20;
int incomeB = classB*15;
int incomeC = classC*10;
int incomeD = classD*5;
System.out.println("Class A Seats income: "+incomeA);
System.out.println("Class B Seats income: "+incomeB);
System.out.println("Class C Seats income: "+incomeC);
System.out.println("Class D Seats income: "+incomeD);
System.out.println("Total income: "+(incomeA+incomeB+incomeC+incomeD));
scanner.close();
}
}
LAB TASK 8
Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Enter a number between 0 and 1000: 999 The sum of the digits is 27
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer between 0 and 1000: ");
int num = scanner.nextInt();
int num1 = num%10;
num = num/10;
int num2 = num%10;
num = num/10;
int num3 = num;
System.out.printf("The sum of %d %d & %d is : %d",num1,num2,num3,(num1+num2+num3));
scanner.close();
}
}
LAB TASK 9
Consider the statements: double x = 75.3987; double y = 982.89764; What is the output of the following statements?
System.out.printf("%.2f %n", x);
System.out.printf("%.2f %n", y);
System.out.printf("%.3f %n", x);
System.out.printf("%.3f %n", y);
CODE:
public class Main {
public static void main(String[] args) {
double x = 75.3987;
double y = 982.89764;
System.out.printf("%.2f %n", x);
System.out.printf("%.2f %n", y);
System.out.printf("%.3f %n", x);
System.out.printf("%.3f %n", y);
}
}
LAB TASK 10
Write JAVA statements using System.out.prinf() statement to display output as given below
Degrees Radians Sine Cosine Tangent
30 0.5236 0.5000 0.8660 0.5773
60 1.0472 0.8660 0.5000 1.7320
public class DemoFormat { public static void main(String[] args) {
// Display the header of the table using System.out.printf()
int degrees = 30;
double radians = Math.toRadians(degrees);
double sin = Math.sin(radians);
double cos = Math.cos(radians);
double tan = Math.tan(radians);
// Display the Data of the table using System.out.printf()
degrees = 60;
radians = Math.toRadians(degrees);
sin = Math.sin(radians); cos = Math.cos(radians);
tan = Math.tan(radians);
// Display the Data of the table using System.out.printf() } }
CODE:
public class Main {
public static void main(String[] args) {
String string1 = "Degrees";
String string2 = "Radians";
String string3 = "Sine";
String string4 = "Cosine";
String string5 = "Tangent";
System.out.printf("%s %-13s %-13s %-13s %-13s \n", string1, string2, string3, string4, string5);
int degrees = 30;
double radians = Math.toRadians(degrees);
double sin = Math.sin(radians);
double cos = Math.cos(radians);
double tan = Math.tan(radians);
System.out.printf("%d %-13.4f %-13.4f %-13.4f %-13.4f\n",degrees,radians,sin,cos,tan);
degrees = 60;
radians = Math.toRadians(degrees);
sin = Math.sin(radians);
cos = Math.cos(radians);
tan = Math.tan(radians);
System.out.printf("%d %-13.4f %-13.4f %-13.4f %-13.4f\n",degrees,radians,sin,cos,tan);
}
}