Skip to content

Instantly share code, notes, and snippets.

@rumaisaabdulhai
Last active January 31, 2021 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumaisaabdulhai/da4db1dc1bad6b06216279f2c9033879 to your computer and use it in GitHub Desktop.
Save rumaisaabdulhai/da4db1dc1bad6b06216279f2c9033879 to your computer and use it in GitHub Desktop.
Week 2 Classwork Solutions
/**
* This class contains the solutions to
* the Week 2 classwork exercises.
*/
public class WeekTwoCW {
public static void main(String[] args) {
/////////////////////////////
// Short Answer Questions //
///////////////////////////
/* 1. Explain the difference between 2, 2.0, "2", and "2.0"
Review: There are 4 main data types you should know:
int, double, char, and String.
- Ints (or integers) are whole numbers like -2, 0, 1, or 2.
They can be positive or negative.
- Doubles (or decimal numbers) are real numbers like -2.41, 7.86,
or 1293.5. They can also be either positive or negative.
- Chars are single characters that you see on your keyboard
like 'A', 'b', '@', or '#'. Some examples are letters or common
symbols. In order to make a char variable, you have to use single
quotes which look like ' ' not " ".
- Finally, Strings are sequences of characters. In other words, they
are a lot of characters combined together. For example, "Hello" is
made from the 'H', 'e', 'l', and 'o' characters.
Answer:
- 2 is an integer. Alternative answers: 2 could also be a byte, short, or long variable
- 2.0 is a double. Alternative answer: 2.0 could also be a float variable
- "2" is a String. If it was '2' instead of "2" it would be a character
- "2.0" has to be a String.
It is very important to understand these differences because even though we think
all of these (above) mean the same thing (2 is a number), the computer thinks that these
are all different values.
Below are examples
*/
// part 1
int apples = 2;
byte oranges = 2; // alternative answer
short berries = 2; // alternative answer
long bananas = 2; // alternative answer
// part 2
double cost = 2.0;
float price = 2.0f; // alternative answer
// part 3
String str = "2";
// part 4
String str2 = "2.0";
/* 2. Cast an integer to a double, and a double to an integer. What happens?
If you cast an integer variable to a double, you do not lose any information.
This is called widening casting.
If you cast a double variable to an integer, that number is truncated - that means
whatever comes after the decimal place is ignored.
Run the program and see what happens to these variables:
*/
System.out.println("\nShort Answer Question 2:\n"); // to separate Problems
int p = 14;
double r = 19.7;
// Cast integer to double
double new_p = p;
// Cast double to integer;
int new_r = (int) r;
System.out.println("The old p value is " + p);
System.out.println("The new p value is " + new_p);
System.out.println("The old r value is " + r);
System.out.println("The new r value is " + new_r);
// Note that when we cast a double to an integer, it does NOT round the number.
// So even if our double is 14.9999, if you cast it to an integer you will get 14.
/* 3. Create a program that says “hello my name is [insert name] I am [insert age]
years old and I want to study [insert field] when I get older”
Note: Try doing this using variables, not just typing the entire phrase into the
System.out.print();
*/
System.out.println("\nShort Answer Question 3:\n"); // to separate Problems
String name = "Rumaisa";
int age = 17;
String field = "robotics";
System.out.println("hello my name is " + name + ". I am " + age +
" years old and I want to study " + field + " when I get older.");
System.out.println("\nClasswork Problem 1\n"); // to separate Problems
//////////////////////////////////////////////////////////////
// Classwork Problem 1: Using Variables & Print Statements //
////////////////////////////////////////////////////////////
// Using the following variables, output in the console
// the sum, subtraction, product, and quotient of a and b. Do the
// same for x and y.
int a = 20;
int b = 11;
double x = 5.34;
double y = 3.4;
int sum1 = a + b;
double sum2 = x + y;
System.out.println("Adding:");
System.out.println("a + b = " + sum1);
System.out.println("x + y = " + sum2);
int sub1 = a - b;
double sub2 = x - y;
System.out.println("Subtracting:");
System.out.println("a - b = " + sub1);
System.out.println("x - y = " + sub2);
int product1 = a * b;
double product2 = x * y;
System.out.println("Multiplying:");
System.out.println("a * b = " + product1);
System.out.println("x * y = " + product2);
int quotient1 = a / b;
double quotient2 = x / y;
System.out.println("Dividing:");
System.out.println("a / b = " + quotient1);
System.out.println("x / y = " + quotient2);
int rem1 = a % b;
double rem2 = x % y;
System.out.println("Remainder");
System.out.println("a % b = " + rem1);
System.out.println("x % y = " + rem2);
///////////////////////////////////////////////////////
// Classwork Problem 2: Order of Operations in Java //
/////////////////////////////////////////////////////
// Using what you learned about the order of operations in java,
// make predictions for all the values below.
// Because these numbers are complex, we will use parentheses to
// explain the order of calculations for each value. I will write
// detailed explanations for the first two problems.
double h = 5.43;
double i = 34.909;
double j = 54.05;
double k = 8594.4;
double value1 = h * i + k / j;
double value2 = h + h % k * k * j - i;
double value3 = i / j % k % h + i * h;
double value4 = k * j % k / k * i;
double value5 = i * j * k % h + j * k;
// Problem: h * i + k / j
// h * i will be calculated first.
// k / j will be calculated next.
// then we will add (h * i) and (k / j)
System.out.println("Prediction 1: " + ( (h * i) + (k / j) ));
System.out.println("h * i + k / j = " + value1);
System.out.println();
// Problem: h + h % k * k * j - i
// h % k will be calculated first.
// next we will multiply (h % k) and k
// then we will multiply ((h % k) * k) and j
// then we will add h and (((h % k) * k) * j)
// finally we will subtract i from (h + (((h % k) * k) * j))
System.out.println("Prediction 2: " + ( (h + (((h % k) * k) * j)) - i ));
System.out.println(" h + h % k * k * j - i = " + value2);
System.out.println();
System.out.println("Prediction 3: " + ( (((i / j) % k) % h) + (i * h) ));
System.out.println("i / j % k % h + i * h = " + value3);
System.out.println();
System.out.println("Prediction 4: " + ( (((k * j) % k) / k) * i ));
System.out.println("k * j % k / k * i = " + value4);
System.out.println();
System.out.println("Prediction 5: " + ( (((i * j) * k) % h) + (j * k) ));
System.out.println("i * j * k % h + j * k = " + value5);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment