Lab 2
PROBLEM: DISPLAY TIME
Write a program that obtains minutes and remaining seconds from seconds
CODE:
import java.util.Scanner;
public class Probem1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input;
int secs;
int mins;
System.out.print("Enter time in seconds: ");
input = scanner.nextInt();
mins = input/60;
secs = input%60;
System.out.printf("The time is %d minutes and %d seconds.",mins,secs);
}
}
PROBLEM: CONVERTING TEMPERATURE
Write a program that converts a Fahrenheit degree to Celsius
CODE:
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double tempinFh;
double tempinCs;
System.out.print("Enter temperature in Farenheit: ");
tempinFh = scanner.nextDouble();
tempinCs = (tempinFh-32)*(5.0/9.0);
System.err.println("The temperature in Celsius is: "+tempinCs);
}
}
CLASS EXERCISE
Write a program that allows the user to enter a value for one edge of a cube. The program calculates the surface area of one side of the cube, the surface area of the cube, and its volume. The program outputs all the results.
CODE:
import java.util.Scanner;
public class Cube {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double edge;
double sideArea;
double cubeArea;
double volume;
System.out.print("Enter value for one edge: ");
edge = scanner.nextDouble();
sideArea = edge*edge;
cubeArea = 6.0*sideArea;
volume = edge*edge*edge;
System.out.printf("Area of one face of cube is: %.2f\nArea of whole cube is: %.2f\nVolume of the cube is: %.2f",sideArea,cubeArea,volume);
}
}
CLASS EXERCISE 2
Write a Java program that calculates a salesperson's total pay based on their base salary, total sales, commission rate, and days absent. Each day absent results in a deduction of 300 rupees. The program should compute the final pay after applying deductions and commissions before displaying the result.
CODE:
import java.util.Scanner;
public class TotalPay {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double totalPay;
double baseSalary;
double comRate;
double totalSale;
int absent;
double loan;
final int absentFine = 300;
final double bonus = 2000;
System.out.print("Enter your base salary: ");
baseSalary = scanner.nextDouble();
System.out.print("Enter the total sale you made: ");
totalSale = scanner.nextDouble();
System.out.print("Enter the commission rate in %: ");
comRate = scanner.nextDouble()/100;
System.out.print("Enter the number of absentees you made: ");
absent = scanner.nextInt();
System.out.print("Enter the loan you pay back per month: ");
loan = scanner.nextDouble();
double deductions = absent*absentFine + loan;
double benefits = comRate*totalSale + bonus;
totalPay = baseSalary + benefits - deductions;
System.out.printf("You will be paid: %.2fPkr\nThe deductions are: %.2fPkr\nThe benefits are: %.2fPkr",totalPay,deductions,benefits);
scanner.close();
}
}
ASSIGNMENT 1
Write a program that accepts a user's monthly pay, rent, utilities, student loans and grocery bills and displays the amount available for discretionary spending (which might be negative). The program is to output the pay, the total bills as well as the remaining discretionary amount.
CODE:
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double pay;
double rent;
double utilities;
double loan;
double grocery;
double discSpend;
double totalBills;
System.out.print("Enter the amount you are paid monthly: $");
pay = scanner.nextDouble();
System.out.print("Enter your monthly rent amount: $");
rent = scanner.nextDouble();
System.out.print("Enter your monthly utilities expense: $");
utilities = scanner.nextDouble();
System.out.print("Enter the amount of loan you repay per month: $");
loan = scanner.nextDouble();
System.out.print("Enter your monthly grocery bill: $");
grocery = scanner.nextDouble();
totalBills = rent + utilities + loan + grocery;
discSpend = pay - totalBills;
System.out.printf("Your total bills are: $%.2f\nAmount available for discretionary spending: $%.2f",totalBills,discSpend);
scanner.close();
}
}
ASSIGNMENT 2
Write a program that takes an amount in decimal representing cents and outputs its breakdown in dollars, 50 cents, 25 cents, and remaining cents. The program should first report the maximum number of dollars, then 50-cent coins, and so on, in this order.
CODE:
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double amount;
int dollars;
int cents50;
int cents25;
int cents10;
int cents5;
int cents;
System.out.print("Enter the amount: $");
amount = scanner.nextDouble();
dollars = (int) amount;
cents50 = (int) ((amount-dollars)/0.5);
cents25 = (int) (((amount-dollars)-cents50*0.5)/0.25);
cents10 = (int) ((((amount-dollars)-cents50*0.5)-cents25*0.25)/0.10);
cents5 = (int) (((((amount-dollars)-cents50*0.5)-cents25*0.25)-cents10*0.10)/0.05);
cents = (int) Math.round((((((amount-dollars)-cents50*0.5)-cents25*0.25)-cents10*0.10)-cents5*0.05)/0.01);
System.out.println("Dollar coins: "+dollars);
System.out.println("50 Cents coin: "+cents50);
System.out.println("25 Cents coin: "+cents25);
System.out.println("10 Cents coin: "+cents10);
System.out.println("5 Cents coin: "+cents5);
System.out.println("Remaining cents: "+cents);
scanner.close();
}
}