Set Probleme Rezolvate 2
Set Probleme Rezolvate 2
* Hey, if you like this, can you like my fb page? I have no idea how to promote
it.
* https://www.facebook.com/MetonymyQT
* I won't spam you, thanks! (you can turn notifications off.)
*/
import java.util.Scanner;
public class ProblemeElementareRezolvate {
public static void main(String[] args) {
// problema1();
// problema2();
// problema3();
// problema4();
// problema5();
// problema6();
// problema7();
// problema8();
// problema9();
//problema10();
problema11();
}
static void problema1() {
/*
* (Compute the volume of a cylinder) Write a program that reads
in the
* radius and length of a cylinder and computes the area and vol
ume
* using the following formulas:
*/
// area = radius^2 * pi
// volume = area * length
Scanner input = new Scanner(System.in);
System.out.println("Enter radius and lenght of a cylinder");
double radius = input.nextDouble();
double length = input.nextDouble();
// compute
double area = Math.pow(radius, 2) * Math.PI;
double volume = area * length;
// display
System.out.println("Area: " + area + " Volume: " + volume);
input.close();
}
static void problema2() {
/*
* (Sum the digits in an integer) Write a program that reads an
integer
* between 0and 1000and adds all the digits in the integer. For
example,
* if an integer is 932, the sum of all its digits is 14.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer between 1 and 1000");
int digit = input.nextInt();
if (digit != 0 && digit <= 1000) {
int result = 0;
int len = (int) Math.log10(digit) + 1;
for (int i = 0; i < len; i++) {
result += digit % 10;
digit /= 10;
}
System.out.println("The result is: " + result);
} else {
System.out.println("You must enter an integet between 1
and 1000!");
}
input.close();
}
static void problema3() {
/*
* Write a program that prompts the user to enter theminutes (e.
g., 1
* billion), and displays the number of years and days for the m
inutes.
* For simplicity, assume a year has 365days.
*/
Scanner input = new Scanner(System.in);
long minutes = input.nextLong();
long totalDays = (minutes / 60) / 24;
long years = totalDays / 365;
long remainingDays = totalDays % 365;
System.out.println(minutes + " is approximately " + years
+ " years and " + remainingDays + " days.");
input.close();
}
static void problema4() {
/*
* (Current time) Listing 2.7, ShowCurrentTime.java, gives a pro
gram
* that displays the current time in GMT. Revise the program so
that it
* prompts the user to enter the time zone offset to GMT and dis
plays
* the time in the specified time zone.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter the time zone offset to GMT:");
int gmt = input.nextInt();
long currentTimeMillis = System.currentTimeMillis();
long totalSecconds = currentTimeMillis / 1000;
long totalMinutes = totalSecconds / 60;
long totalHours = totalMinutes / 60;
long secconds = totalSecconds % 60;
long minutes = totalMinutes % 60;
long hours = (totalHours % 24) + gmt;
System.out.print("The current time is: ");
System.out.println(hours + ":" + minutes + ":" + secconds);
input.close();
}
static void problema5() {
/*
* (Financial application: compound value) Suppose you save
* $100eachmonth into a savings account with the annual interest
rate
* 5%. Thus, the monthly interest rate is 0.05/12 =0.00417. Afte
r the
* first month, the value in the account becomes 100 * (1 + 0.00
417) =
* 100.417 After the second month, the value in the account beco
mes (100
* + 100.417) * (1 + 0.00417) = 201.252 After the third month, t
he value
* in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.
507 and
* so on. Write a program that prompts the user to enter a month
ly
* saving amount and displays the account value after the sixth
month.
* (In Exercise 5.30, you will use a loop to simplify the code a
nd
* display the account value for any month.)
*/
Scanner input = new Scanner(System.in);
System.out.print("Enter the monthly saving amount: ");
int amount = input.nextInt();
double accountValue = 0;
String monthWord = "";
double interestRate = 1 + 0.00417;
for (int i = 1; i <= 6; i++) {
switch (i) {
case 1:
monthWord = "first";
break;
case 2:
monthWord = "second";
break;
case 3:
monthWord = "third";
break;
case 4:
monthWord = "fourth";
break;
case 5:
monthWord = "fifth";
break;
case 6:
monthWord = "sixth";
break;
default:
break;
}
accountValue = (accountValue + amount) * interestRate;
System.out.format("After the %s month, the account value
is $%.3f",
monthWord, accountValue);
System.out.println();
input.close();
}
}
static void problema6() {
/*
* (Health application: computing BMI) Body Mass Index (BMI) is
a
* measure of health on weight. It can be calculated by taking y
our
* weight in kilograms and dividing by the square of your height
in
*
* meters. Write a program that prompts the user to enter a weig
ht in
* pounds and height in inches and displays the BMI. Note that o
ne pound
* is 0.45359237kilograms and one inch is 0.0254meters.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter weight in pounds:");
double inputPounds = input.nextDouble();
System.out.println("Enter height in inches:");
double inputInches = input.nextDouble();
double kilograms = inputPounds * 0.45359237;
double meters = inputInches * 0.0254;
double bmi = kilograms / Math.pow(meters, 2);
System.out.println();
System.out.format("BMI is: %.4f", bmi);
input.close();
}
static void problema7() {
/*
* (Science: wind-chill temperature) How cold is it outside? The
* temperature alone is not enough to provide the answer. Other
factors
* including wind speed, relative humidity, and sunshine play im
portant
* roles in determining coldness outside. In 2001, the National
Weather
* Service (NWS) implemented the new wind-chill temperature to m
easure
* the coldness using temperature and wind speed.
*
* The formula is t wc=35.74+0.6215t a -35.75v 0.16 +0.4275t av
0.16
* where t a is the outside temperature measured in degrees Fahr
enheit
* and vis the speed measured in miles per hour. twc is the wind
-chill
* temperature. The formula cannot be used for wind speeds below
2 mph
* or temperatures below -58 F or above 41F. Write a program that
* prompts the user to enter a temperature between -58 F and 41F a
nd a
* wind speed greater than or equal to 2and displays the wind-ch
ill
* temperature. Use Math.pow(a, b)to compute v 0.16 .
*/
Scanner input = new Scanner(System.in);
System.out
.println("Enter the temperature in Fahrenheit be
tween -58F and 41F: ");
double temperature = input.nextDouble();
System.out.println("Enter the wind speed (>=2) in miles per hour
: ");
double windSpeed = input.nextDouble();
boolean legit = temperature > -58 && temperature < 41 ? true : f
alse;
if (legit && windSpeed >= 2) {
double computedWindSpeed = Math.pow(windSpeed, 0.16);
double windChillIndex = 35.74 + 0.6215 * temperature - 3
5.75
* computedWindSpeed + 0.4276 * temperatu
re
* computedWindSpeed;
System.out.format("The wind chill index is %.5f", windCh
illIndex);
} else {
System.out.println("Error: You must follow the instructi
ons!");
}
input.close();
}
static void problema8() {
/*
* Geometry: area of a triangle) Write a program that prompts th
e user
* to enter three points (x1, y1), (x2, y2), (x3, y3)of a triang
le and
* displays its area.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
double side1x = input.nextDouble();
double side1y = input.nextDouble();
double side2x = input.nextDouble();
double side2y = input.nextDouble();
double side3x = input.nextDouble();
double side3y = input.nextDouble();
double side1 = Math.sqrt(Math.pow((side1x - side2x), 2)
+ Math.pow((side1y - side2y), 2));
double side2 = Math.sqrt(Math.pow((side1x - side3x), 2)
+ Math.pow((side1y - side3y), 2));
double side3 = Math.sqrt(Math.pow((side2x - side3x), 2)
+ Math.pow((side2y - side3y), 2));
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - sid
e3));
input.close();
System.out.println("The area of the triangle is " + area);
input.close();
}
static void problema9() {
/*
* (Financial application: calculate interest) If you know the b
alance
* and the annual percentage interest rate, you can compute the
interest
* on the next monthly payment using the following formula: inte
rest
* =balance*(annualInterestRate/1200) Write a program that reads
the
* balance and the annual percentage interest rate and displays
the
* interest for the next month. Here is a sample run:
*/
Scanner input = new Scanner(System.in);
System.out
.println("Enter balance and interest rate (e.g.,
3 for 3%): ");
double balance = input.nextDouble();
double interestRate = input.nextDouble();
double interest = balance * (interestRate / 1200);
System.out.format("The interest is %.5f", interest);
input.close();
}
static void problema10() {
/*
* (Financial application: calculate future investment value) Wr
ite a
* program that reads in investment amount, annual interest rate
, and
* number of years, and displays the future investment value usi
ng the
* following formula: futureInvestmentValue = investmentAmount *
(1
* +monthlyInterestRate) numberOfYears*12 For example, if you en
ter
* amount 1000, annual interest rate 3.25%, and number of years
1, the
* future investment value is 1032.98.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
double investmentAmount = input.nextDouble();
System.out.println("Enter annual interest rate in percentage: ")
;
double anualInterestRate = input.nextDouble();
System.out.println("Enter number of years: ");
double numberOfYears = input.nextDouble();
double montlyInterestRate = anualInterestRate / 12;
double futureInvestmentValue = investmentAmount
+ Math.pow((1 + montlyInterestRate), (numberOfYe
ars * 12));
System.out.println("Accumulated value is " + futureInvestmentVal
ue);
input.close();
}
static void problema11() {
/*
* (Cost of driving) Write a program that prompts the user to en
ter the
* distance to drive, the fuel efficiency of the car in miles pe
r
* gallon, and the price per gallon, and displays the cost of th
e trip.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter the driving distance:");
double distance = input.nextDouble();
System.out.println("Enter miles per gallon:");
double miles = input.nextDouble();
System.out.println("Enter price per gallon:");
double price = input.nextDouble();
double cost = distance / miles * price;
System.out.println("The cost of driving is " + cost);
input.close();
}
}