Assignment 1 Java Code
Assignment 1 Java Code robert gant cosc236 (108) assignment write java program that will ask the user to type in number of seconds, and then convert that number
https://d20ohkaloyme4g.cloudfront.net/img/favicon.svg
Studocu
Robert Gant COSC236 (108) Assignment 1

1. Write a Java program that will ask the user to type in a number of seconds, and then

convert that number into the appropriate number of hours, minutes, and seconds.

//Robert Gant COSC236 (108) Assignment 1 import java.util;

public class TimeConverter { public static void main(String[] args) { Scanner scanner = new Scanner(System);

// Prompt the user for input System.out("Enter a number of seconds: "); int totalSeconds = scanner();

// Calculate hours, minutes, and remaining seconds int hours = totalSeconds / 3600; int minutes = (totalSeconds % 3600) / 60; int seconds = totalSeconds % 60;

// Print the converted time System.out("%d seconds is equivalent to %d hours, %d minutes, and %d seconds.", totalSeconds, hours, minutes, seconds);

scanner(); }

2. Write a Java program that takes as input any change expressed in cents and

computes the number of half-dollars, quarters, dimes, nickels, and pennies to be

returned. Use as many half-dollars as possible, then quarters, dimes, nickels, and

pennies, in that order.

//Robert Gant COSC236 (108) Assignment 1

import java.util;

public class ChangeCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System);

// Prompt the user for input

System.out("Enter the amount of change in cents: ");

int change = scanner();

// Calculate the number of half-dollars, quarters, dimes, nickels, and pennies

int numHalfDollars = change / 50;

change %= 50;

int numQuarters = change / 25;

change %= 25;

int numDimes = change / 10;

change %= 10;

int numNickels = change / 5;

change %= 5;

int numPennies = change;

// Print the result

System.out("You will receive %d half-dollars, %d quarters, %d dimes,

%d nickels, and %d pennies.",

numHalfDollars, numQuarters, numDimes, numNickels,

numPennies);

scanner();

}

The net pay is calculated after taking the following deductions:

Federal income tax = 15%

State income tax = 5%

Social security tax = 5%

Medicare/Medicaid = 3%

Pension plan = 7%

Health insurance = $125.

Your program should ask the user to input the employee ID, last name, first name, and the

earnings (gross amount) for current month. The output should be the net pay, after deductions.

Format the output to have 2 decimal places. NOTE: This assignment is all about formatting the

output.

//Robert Gant COSC236 (108) Assignment 1 import java.util;

public class PaycheckCalculator { public static void main(String[] args) { // Initialize constants for tax rates and health insurance final double FEDERAL_TAX_RATE = 0; final double STATE_TAX_RATE = 0; final double SOCIAL_SECURITY_TAX_RATE = 0; final double MEDICARE_TAX_RATE = 0; final double PENSION_PLAN_RATE = 0; final double HEALTH_INSURANCE = 125;

// Ask user to input employee information and earnings Scanner scanner = new Scanner(System); System.out("Enter employee ID: "); int employeeId = scanner(); System.out("Enter employee last name: "); String lastName = scanner(); System.out("Enter employee first name: "); String firstName = scanner(); System.out("Enter gross earnings for the month: "); double grossEarnings = scanner();

// Calculate deductions double federalTax = grossEarnings * FEDERAL_TAX_RATE; double stateTax = grossEarnings * STATE_TAX_RATE; double socialSecurityTax = grossEarnings * SOCIAL_SECURITY_TAX_RATE; double medicareTax = grossEarnings * MEDICARE_TAX_RATE; double pensionPlan = grossEarnings * PENSION_PLAN_RATE; double totalDeductions = federalTax + stateTax + socialSecurityTax + medicareTax + pensionPlan + HEALTH_INSURANCE; double netPay = grossEarnings - totalDeductions;

// Print paycheck in table format System.out("\nPaycheck for Employee #" + employeeId + " - " + lastName + ", " + firstName);

System.out("=========================================== =========="); System.out("%-20s %10s\n", "Gross Earnings:", "$" + String("%", grossEarnings)); System.out("%-20s %10s\n", "Federal Tax:", "$" + String("%", federalTax)); System.out("%-20s %10s\n", "State Tax:", "$" + String("%", stateTax)); System.out("%-20s %10s\n", "Social Security Tax:", "$" + String("%", socialSecurityTax)); System.out("%-20s %10s\n", "Medicare/Medicaid Tax:", "$" + String("%", medicareTax)); System.out("%-20s %10s\n", "Pension Plan:", "$" + String("%", pensionPlan)); System.out("%-20s %10s\n", "Health Insurance:", "$" + String("%", HEALTH_INSURANCE)); System.out("%-20s %10s\n", "Total Deductions:", "$" + String("%", totalDeductions));

System.out("=========================================== =========="); System.out("%-20s %10s\n", "Net Pay:", "$" + String("%", netPay)); }