← Back to feed
Renewal·서른의 생활코딩

Day 5) Operator Review Examples _20180531 am

NS
normalstory
cover image

package z_exam;


public class exam03 {

public static void main(String[] args){

// [3-1] Write the result of the following operations.

// [Exercise]/ch3/Exercise3_1.java

// class Exercise3_1 {

// public static void main(String[] args) {

int x = 2;

int y = 5;

char c = 'A'; // 'A' char code is 65

System.out.println(1 + x << 33);  

// 1+2<<33 = int 3<<3 = 11(2)<<33 

// shift 32 bits left by 33 = 10(2) = 2 -wrong

System.out.println(y >= 5 || x < 0 && x > 2);

// 2<0 && 2>2 || 5<=5 

// f && f || t = f || t = true

System.out.println(y += 10 - x++);

// 15 - 2 =13  //(x=3, y=15) 

System.out.println(x+=2);

// (x=3, y=15)+2 = 5

System.out.println( !('A' <= c && c <='Z') );

// !(65<=65 && 65 <='Z') = !(t && t) = false

System.out.println('C'-c);

// 67-65 = 2

System.out.println('5'-'0');

// 5

System.out.println(c+1);

// char + int = 66 

System.out.println(++c);

// char 67 // ++ (increment) is a position shift, not +1 (int)?

System.out.println(c++);

// char 67

System.out.println(c);

// char 68

// }

// }

// [3-2] The code below calculates the number of baskets (buckets) needed to hold apples.

// If there are 123 apples and each basket holds 10, then 13 baskets

// are needed. Fill in (1) with the appropriate code.

// [Exercise]/ch3/Exercise3_2.java

// class Exercise3_2 {

// public static void main(String[] args) {

int numOfApples = 121; // number of apples

int sizeOfBucket = 10; // bucket size (apples per bucket)

int numOfBucket1 = (numOfApples/sizeOfBucket+1); // number of buckets needed

//Method 1 — base 10, so just +1 and done

System.out.println("Method 1, buckets needed :"+numOfBucket1);

int numOfBucket2 = (int)((float)numOfApples/sizeOfBucket+0.9f);//unit is 1, so 0.9

//Method 2 — accounts for varying bucket sizes

//??? question: what if the unit isn't 1 but 16?

System.out.println("Method 2, buckets needed :"+numOfBucket2);

//Method 3 — use ternary operator (=if, when condition result is t or f) -> handle cases with and without a remainder

int numOfBucket3 = numOfApples/sizeOfBucket+(numOfApples%sizeOfBucket>0 ? 1:0);

System.out.println("Method 3, buckets needed :"+numOfBucket3);

// }

// }

// [Output]

// 13

// [3-3] The code below prints 'positive', 'negative', or '0' based on the value of num.

// Use the ternary operator and fill in (1).

// [Hint] Use the ternary operator twice.

// [Exercise]/ch3/Exercise3_3.java

// class Exercise3_3 {

// public static void main(String[] args) {

int num3 = 10;

System.out.println( num3==0 ? "0" : num3<0 ? "negative" : "positive");

// num3<0 ? "negative" : "positive" //first translate the problem into code

// num3==0 ? "zero" : "non-zero"  //but 'non-zero' covers a wider range than pos/neg, so move it to the front

// }

// }

// [3-4] The code below drops digits below the hundreds place of num.

// If num is '456' it becomes '400'; if it's '111' it becomes '100'. Fill in (1).

//

// [Exercise]/ch3/Exercise3_4.java

// class Exercise3_4 {

// public static void main(String[] args) {

int num4 = 456;

System.out.println( (int)(num4/100.0f) * 100 ); 

//In order: shift below the hundreds into decimals, then cast to drop the remainder

//num4/100.0f

//(int)(num4/100.0f)

//(int)(num4/100.0f) * 10

// }

// }

// [3-5] The code below replaces the ones digit of num with 1.

// If num is 333 it becomes 331; if 777, it becomes 771. Fill in (1).

// [Exercise]/ch3/Exercise3_5.java

// class Exercise3_5 {

// public static void main(String[] args) {

int num5 = 333;

System.out.println( (int)(num5/10.0f)*10+1 );

//num5/10.0f

//(int)(num5/10.0f)

//(int)(num5/10.0f)*10

//(int)(num5/10.0f)*10 +1

// }

// }

// [Output]

// positive

// [Output]

// 400

// [Output]

// 331

/*

[3-6] Below computes the remainder after subtracting num from the nearest multiple of 10 

that is greater than num. 

//target value is the remainder  == y=(%) = 10x - num

//value of (some x, a multiple of 10) - num == 10x - num

//x, a multiple of 10, is close to and larger than num == x=(int)(num/10.0f)+1

For example, the nearest multiple of 10 greater than 24 is 30.

For 19 it's 20; for 81 it's 90. 30 minus 24 is 6, so when num is 24 the result must be 6.

Fill in (1).*/

// [Hint] Use the modulo operator.

// [Exercise]/ch3/Exercise3_6.java

// class Exercise3_6 {

// public static void main(String[] args) {

int num6 = 24;

System.out.println( 10-(num6%10) );

// Method 1) compute remainder on the last digit in base 10 

System.out.println( (((int)(num6/10.0f)+1) *10) - num6);

// Method 2) accounts for a variable base.. 

// }

// }

// [3-7] Below converts Fahrenheit to Celsius. Given the formula 

// 'C = 5/9 ×(F - 32)', fill in (1). 

// Note: round at the 3rd decimal place without using Math.round().

// [Exercise]/ch3/Exercise3_7.java

// class Exercise3_7 {

// public static void main(String[] args) {

int fahrenheit = 100;

float celcius = ( (int)(5.0f/9 *(fahrenheit-32)*1000) /1000.0f );

//C = 5/9 x (F-32) //given formula

//5/9 *(fahrenheit-32) //coded up

//5.0f/9 *(fahrenheit-32) //apply as code

//(5.0f/9 *(fahrenheit-32)*1000) shift up to 3 decimals

//(int)(5.0f/9 *(fahrenheit-32)*1000) cast to int, drop below 3rd decimal

//(int)(5.0f/9 *(fahrenheit-32)*1000) /1000.0f restore original scale

System.out.println("Fahrenheit:"+fahrenheit);

System.out.println("Celcius:"+celcius);

// }

// }

// [Output]

// 6

// [Output]

// Fahrenheit:100

// Celcius:37.78

// [3-8] Fix the problems in the following code to produce the expected output.

// [Exercise]/ch3/Exercise3_8.java

// class Exercise3_8 {

// public static void main(String[] args) {

// byte a = 10;

// byte b = 20;

// byte c = a + b;

// char ch = 'A';

// ch = ch + 2;

// float f = 3 / 2;

// long l = 3000 * 3000 * 3000;

// float f2 = 0.1f;

// double d = 0.1;

// boolean result = d==f2;

// System.out.println("c="+c);

// System.out.println("ch="+ch);

// System.out.println("f="+f);

// System.out.println("l="+l);

// System.out.println("result="+result);


byte a = 10;

byte b = 20;

int c8 = a + b;    //put the container into int, not byte — in + operations a and b are both promoted to int anyway

//variable name c was used already, so renamed to c8

char ch = 'A';

ch = (char)(ch + 2);// Method 1) cast based on char (since we took a char as input)

float f = 3.0f / 2; // cast before the assignment (=) so we can keep the remainder 

//-> 3.0f or 2.0f

long l = (long)(3000.0 * 3000 * 3000); 

//the value exceeds the type's size (11 digits)

//long l2 = 27000000000; <- error 

float f2 = 0.1f;

double d = 0.1;

boolean result = (float)d==f2; 

//keeps the input variables untouched 

//to adjust only the result, match both sides before the == operation

System.out.println("c="+c8);

System.out.println("ch="+ch);

System.out.println("f="+f);

System.out.println("l="+l); 

System.out.println("result="+result); //false <- when the value is 0.1, double is a very close approximation

// }

// }

// [Output]

// c=30

// ch=C

// f=1.5

// l=27000000000

// result=true

// [3-9] The code below sets b to true only when character variable ch 

// is an English letter (upper or lower) or a digit.  //<- condition!

// Fill in (1).

// [Exercise]/ch3/Exercise3_9.java

// class Exercise3_9 {

// public static void main(String[] args) {

char ch9 = 's';

boolean b9 = ( 'A'<= ch9 && ch9 <='Z' || 'a'<= ch9 && ch9 <='z' || '0'<= ch9 && ch9 <='9'  );

System.out.println(b9);

// b = is English letter || b = is digit ? true : false

// 'A'<= ch9 <='Z' || 'a'<= ch9 <='z' || b=digit    //with boolean involved, '? :' can be omitted

// upper/lower first: 'A'<= ch9 && ch9 <='Z' || 'a'<= ch9 && ch9 <='z'

// and digits: '0'<= ch9 && ch9 <='9' 

// }

// }

// [3-10] Below converts uppercase to lowercase, 

// "only when ch is uppercase".  <- condition

// Lowercase char codes are 32 higher than uppercase. 

// For example, 'A' is 65 and 'a' is 97. Fill in (1)~(2).

// [Exercise]/ch3/Exercise3_10.java

// class Exercise3_10 {

// public static void main(String[] args) {

char ch10 = 'A';

char lowerCase = ('A'<= ch10 && ch10 <='Z') ? (char)(ch10 + 32) : ch10;

System.out.println("ch10:"+ch10);

System.out.println("ch10 to lowerCase:"+lowerCase);

// (x == uppercase?) ? convert to lowercase (x_uppercase + 32) : keep as-is (x_lowercase) 

//nope! ( 'A' <= ch10 <= 'B' ) ? ch10 + 32 : ch10 

//nope! ch10 + 32 = char + int -> first compute (int + int) then cast back (char) -> (char)(int)

//before that!!!!

//before the '<=' operation, promote ch to int (4-byte)...

//wait, no.. huh... lol — comparison works as long as types match, right?.. 

//nope, auto-promoted since it's under 4 bytes! So it's fine 

// inside ()? you can only do one operation at a time!

//'A'<= ch9 && ch9 <='Z' 

// }

// }

// [Output]

// ch:A

// ch to lowerCase:a

}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.