-
-
Save rumaisaabdulhai/e2fed94785dca262520bf8dd92330fe4 to your computer and use it in GitHub Desktop.
Week 2 Homework Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class contains the solutions to | |
* the Week 2 homework exercises. | |
*/ | |
public class WeekTwoHW { | |
public static void main(String[] args) { | |
// -------- MANDATORY PROBLEMS -------- | |
/* | |
Problem 1. Write a Java program to declare three variables: | |
bananas, budget, and cost and store the values 10, 20.3, and | |
3.14785, respectively. | |
*/ | |
int bananas = 10; // integer because you cannot have part of a banana | |
double budget = 20.3; // budget & cost must be decimal numbers | |
double cost = 3.14785; | |
System.out.println("--- MANDATORY PROBLEMS ---"); | |
System.out.println("\nProblem 1:\n"); | |
System.out.println("\tBananas: " + bananas); | |
System.out.println("\tBudget: " + budget); | |
System.out.println("\tCost: " + cost); | |
/* | |
Problem 2: Write a Java program to declare a Boolean variable | |
with initial value of “true.” Change it to “false” before printing it. | |
*/ | |
boolean bool = true; // you can name your boolean variable anything | |
bool = false; | |
System.out.println("\nProblem 2:\n"); | |
System.out.println("\tBoolean value: " + bool); | |
/* | |
Problem 3: Write a Java program to convert minutes into a number of days and years. | |
Hint: make 3 integer variables that store the number of minutes, days, and years | |
*/ | |
// CONSTANTS - optional variables | |
final int min_per_hr = 60; | |
final int hrs_per_day = 24; | |
final int days_per_yr = 365; | |
// MINUTES TO DAYS: | |
// 1 hr 1 day | |
// M min x ------ x ------ | |
// 60 min 24 hrs | |
// MINUTES TO YEARS: | |
// 1 hr 1 day 1 yr | |
// M min x ------ x ------ x ------- | |
// 60 min 24 hrs 365 days | |
double minutes = 525600*4; // change the number here to see what happens | |
double hrs = minutes / min_per_hr; // optional variable | |
double days = hrs / hrs_per_day; | |
double years = days / days_per_yr; | |
System.out.println("\nProblem 3:\n"); | |
System.out.println("\tMinutes: " + minutes); | |
System.out.println("\tDays: " + days); | |
System.out.println("\tYears: " + years); | |
// -------- OPTIONAL PROBLEMS -------- | |
/* | |
Problem 1: Write a Java program that takes two variables, a and b, and | |
then prints the sum, the difference, the product, the average, the distance | |
(the difference between integer), the maximum (the larger of the two integers), | |
the minimum (smaller of the two integers). You should have variables that will | |
store each answer. | |
NOTE: There can be alternate solutions to these problems | |
*/ | |
// try changing the numbers to see what happens | |
double a = 12; | |
double b = 18; | |
double sum = a + b; | |
double diff = a - b; | |
double avg = sum / 2; // (a + b)/2 is also correct | |
double dist = Math.abs(diff); // b - a is also correct | |
double max = Math.max(a, b); // b is also correct | |
double min = Math.min(a, b); // a is also correct | |
// Math.abs() is a method from Java that gives you the absolute value | |
// of a number. If -1 is passed in, it returns 1. If 4 is passed in, | |
// it returns 4. It turns a negative number into a positive one. | |
// More info here: https://www.w3schools.com/java/ref_math_abs.asp | |
// Math.max() is a method from Java that gives you the larger of two numbers. | |
// More info here: https://www.w3schools.com/jsref/jsref_max.asp | |
// Math.min() is a method from Java that gives you the smaller of two numbers. | |
// More info here: https://www.w3schools.com/jsref/jsref_min.asp | |
System.out.println("\n--- OPTIONAL PROBLEMS ---"); | |
System.out.println("\nProblem 1:\n"); | |
System.out.println("\tSum: " + sum); | |
System.out.println("\tDifference: " + diff); | |
System.out.println("\tAverage: " + avg); | |
System.out.println("\tDistance: " + dist); | |
System.out.println("\tMaximum: " + max); | |
System.out.println("\tMinimum: " + min); | |
/* | |
Problem 2: Write a Java program to break an integer into a | |
sequence of individual digits (example: 54321 to 5 4 3 2 1) | |
NOTE: Assume it will ALWAYS ask for a 5 digit number | |
*/ | |
// Try entering another 5 digit number to see what happens | |
int num1 = 54321; | |
int ones = num1 % 10; // 1 | |
System.out.println("\nProblem 2:\n"); | |
System.out.println("\tOnes digit: " + ones); | |
int num2 = num1 / 10; // 5432.1 will be cut down to 5432 | |
int tenth = num2 % 10; // 2 | |
System.out.println("\tTenth digit: " + tenth); | |
int num3 = num2 / 10; // 543.2 will be cut down to 543 | |
int hundredth = num3 % 10; // 3 | |
System.out.println("\tHundredth digit: " + hundredth); | |
int num4 = num3 / 10; // 54.3 will be cut down to 54 | |
int thousandth = num4 % 10; // 4 | |
System.out.println("\tThousandth digit: " + thousandth); | |
int num5 = num4 / 10; // 5.4 will be cut down to 5 | |
int ten_thousandth = num5 % 10; // 5 | |
System.out.println("\tTen Thousandth digit: " + ten_thousandth); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment