






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







import java.util.Scanner; public class Chapter_2_1 { public static void main (String[] args){ double celsius; double fahrenheit; System.out.print("Enter degree in celsius: "); Scanner input = new Scanner(System.in); celsius = input.nextDouble(); fahrenheit = (9.0 / 5.0) * celsius + 32.0; System.out.printf ("%1.02f", fahrenheit); System.out.print(" degrees fahrenheit"); input.close(); } } import java.util.Scanner; public class Chapter_2_2 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the radius and length of a cylinder: "); double radius = input.nextDouble(); double length = input.nextDouble(); double area = radius * radius * Math.PI; double volume = area * length; System.out.print("The area of a cylinder is "); System.out.printf("%.2f", area); System.out.println(); System.out.print("The volume of a cylinder is "); System.out.printf("%.2f", volume); input.close(); } }
import java.util.Scanner; public class Chapter_2_3 { public static void main (String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a value for feet: "); double feet = input.nextDouble(); double meters; meters = .305 * feet; System.out.println(feet + " feet is " + meters + " meters."); input.close(); } } import java.util.Scanner; public class Chapter_2_4 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a value for pounds: "); double pounds = input.nextDouble(); double kilograms; kilograms = .454 * pounds; System.out.println(pounds + " pounds is " + kilograms + " kilograms"); input.close(); } } import java.util.Scanner; public class Exercise02_05 { public static void main(String args[]) {
import java.util.Scanner; public class Exercise02_07 { public static void main(String args[]) { final int MinutesinHours = 60; final int HoursinDays = 24; final int DaysinYear = 365; Scanner input = new Scanner(System.in); System.out.println("Enter the number of minutes: "); int numberofMinutes = input.nextInt(); int numberofYears = numberofMinutes / MinutesinHours/ HoursinDays / DaysinYear ; int numberofDays = numberofMinutes / MinutesinHours/ HoursinDays % DaysinYear ; System.out.println(numberofMinutes + " minutes is approximately " + numberofYears + " years and " + numberofDays + " days" ); } } import java.util.Scanner; public class Exercise02_08 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter the time zone offset to GMT: "); int offset = input.nextInt(); long totalMilliseconds = System.currentTimeMillis(); long totalseconds = totalMilliseconds / 1000; long currentseconds = totalseconds % 60; long totalminutes = totalseconds / 60; long currentminute = totalminutes % 60;
long totalhours = totalminutes / 60; long currenthours = totalhours % 24; currenthours = currenthours + offset; System.out.println("Current time is " + currenthours + ":" +currentminute + ":" + currentseconds); } } import java.util.Scanner; public class Exercise02_09 { public static void main(String args[]) { double v0,v1,t; Scanner input = new Scanner(System.in); System.out.println("Enter v0, v1, and t: "); v0 = input.nextDouble(); v1 = input.nextDouble(); t = input.nextDouble(); double a = (v1 - v0) / t; System.out.printf("The average acceleration is %2.4f", a); } } import java.util.Scanner; public class Exercise02_10{ public static void main(String[] args){ double initialTemperature; double finalTemperature; double weightofwater; System.out.println("Enter the weight of the water in Kilograms"); Scanner input = new Scanner(System.in);
double interestPerMonth = 0.05 / 12; double interestFactor = 1 + interestPerMonth; int count = 6; double total = 0; while (count != 0) { total = (total + monthDeposit) * interestFactor; --count; } System.out.print(" At a 5% interest rate, you will have $ "); System.out.printf("%.2f", total); System.out.print(" in your saving account after six months "); } } import java.util.Scanner; public class Exercise02_14 { public static void main(String args[]) { Scanner input = new Scanner(System.in); final double KilogramsPerPound = 0.45359237; final double MetersperInch = 0.0254; System.out.println("Enter weight in pounds: "); double weight = input.nextDouble(); System.out.println("Enter height in inches: "); double height = input.nextDouble(); weight = weight * KilogramsPerPound; height = height * MetersperInch; double bodyMassIndex = weight/Math.pow(height, 2); System.out.println("BMI is " + bodyMassIndex); } }
import java.util.Scanner; public class Exercise_02_15 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter x1 and y1: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); System.out.print("Enter x2 and y2: "); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double distance = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5); System.out.println("The distance between the two points is "+distance); } } import java.util.Scanner; public class Exercise02_16 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter the side: "); double side = input.nextDouble(); double area = ((3 * Math.pow(3, 0.5)) / 2) * Math.pow(side, 2); System.out.println("The area of the hexagon is " + area); } } import java.util.Scanner;
public class Exercise02_19 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three points for a triangle: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double x3 = input.nextDouble(); double y3 = input.nextDouble(); double side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5); double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5); double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5); double s = (side1 + side2 + side3) / 2; double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5); System.out.println("The area of the triangle is " + area); } } import java.util.Scanner; public class Exercise02_21 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter investment amount: "); double amount = input.nextDouble(); System.out.print("Enter annual interest rate in percentage: "); double monthlyInterestRate = input.nextDouble(); monthlyInterestRate /= 1200; System.out.print("Enter number of years: "); int years = input.nextInt(); double futureInvestmentValue = amount * Math.pow(1 + monthlyInterestRate, years * 12); System.out.println("Accumulated value is $" + futureInvestmentValue);
import java.util.Scanner; public class Exercise02_22 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter an amount of integer, for example 1156 for 11.56: "); int amount = input.nextInt(); int remainingAmount = amount; int numberofOneDollars = remainingAmount / 100; remainingAmount %= 100; int numberofQuarters = remainingAmount / 25; remainingAmount %= 25; int numberofDimes = remainingAmount / 10; remainingAmount %= 10; int numberofNickels = remainingAmount / 5; remainingAmount %= 5; int numberofPennies = remainingAmount; System.out.println("Your amount " + amount/100.0 + " consist of "); System.out.println("" + numberofOneDollars + " dollars" ); System.out.println("" + numberofQuarters + " quarters " ); System.out.println("" + numberofDimes + " dimes " ); System.out.println("" + numberofNickels + " nickels " ); System.out.println("" + numberofPennies + " pennies " ); } } import java.util.Scanner;