Академический Документы
Профессиональный Документы
Культура Документы
2nd Session
Alan Kuo
Haiyu Huang
You want to roll dice 100 times and get the average value.
Whats the best while loop to use?
int count = 0;
int sum = 0;
Random gen = new Random();
???????;
double average = (double)sum/ count;
You want to roll dice 100 times and get the average value.
Whats the best while loop to use?
int count = 0;
int sum = 0;
Random gen = new Random();
???????;
double average = (double)sum/ count;
p.setRed(p.getRed() * (int)0.999);
p.setBlue(p.getBlue() * (int)0.999);
p.setGreen(p.getGreen() + 60);
p.setGreen(p.getGreen() * (int)1.5);
A, B, and C
A and B
p.setRed(p.getRed() * (int)0.999);
p.setBlue(p.getBlue() * (int)0.999);
p.setGreen(p.getGreen() + 60);
p.setGreen(p.getGreen() * (int)1.5);
A, B, and C
A and B
A.
B.
C.
D.
E.
-value
255 + value
255 - value
value - 255
None of these
A.
B.
C.
D.
E.
-value
255 + value
255 - value
value - 255
None of these
A.
B.
C.
D.
A.
B.
C.
D.
A.
B.
C.
D.
E.
A.
B.
C.
D.
E.
A.
B.
C.
D.
E.
x % 5 == 0 &&
x % 5 == 0 ||
x / 5 == 0 &&
x / 5 == 0 ||
None of these
y
y
y
y
%
%
/
/
5
5
5
5
==
==
==
==
0
0
0
0
example
A.
B.
C.
D.
E.
x % 5 == 0 &&
x % 5 == 0 ||
x / 5 == 0 &&
x / 5 == 0 ||
None of these
y
y
y
y
%
%
/
/
5
5
5
5
==
==
==
==
0
0
0
0
example
A. Yes
B. No
A. Yes
B. No
A. Yes
B. No
A. Yes
B. No
A.
Compile error
B.
C.
D.
A.
Compile error
B.
C.
D.
Compile error
Runtime error if you ever try to use them
in your methods without initializing them
first somewhere
They take on some default value,
depending on the variable type
This is terrible practice
Compile error
Runtime error if you ever try to use them
in your methods without initializing them
first somewhere
They take on some default value,
depending on the variable type
This is terrible practice
Memory Models
Concepts:
Heap
Return Values
public static int something(int value){
int value2 = value;
return value2+1;
}
public static void main(String[] args){
int value = 50;
System.out.println(something(value));
System.out.println(value);
}
Return Values
public static int something(int value){
int value2 = value;
return value2+1;
}
public static void main(String[] args){
int value = 50;
System.out.println(something(value));
System.out.println(value);
}
Heap
Stack
circle1
radius
color
Heap
Stack
circle1
radius
color
temp
5 10
Heap
Stack
radius
circle1
color
temp
radius
10
5 10
Heap
Stack
radius
circle1
color
temp
radius
10 12
5 10 12
Heap
Stack
radius 5 10
circle1
color
temp
radius
radius
5 12
color
12
tom
Cat
x
y
0
0
Cat
x
y
0
0
Lets make a constructor for our class Person. What could the header look like?
A.
B.
C.
D.
public
public
public
public
Lets make a constructor for our class Person. What could the header look like?
A.
B.
C.
D.
public
public
public
public
Whats the most reasonable way to initialize just the picture instance
variable in this constructor?
A.
B.
C.
D.
E.
this.picture = picture;
picture = picture;
this.picture = new Picture(picture);
this.picture = new Picture(this.picture);
None of these
An example person
Whats the most reasonable way to initialize just the picture instance
variable in this constructor?
A.
B.
C.
D.
E.
An example person
This could work too, but since you are passing a memory location it is in
danger of being unintentionally changed.
this.picture = picture;
picture = picture;
this.picture = new Picture(picture);
this.picture = new Picture(this.picture);
None of these
Bit Shifting
num >> n
num % (1 << n)
num % n
(num >> n) << n
(num << n) >> n
01100100,
01100001,
01101110,
01101011,
01110100
01110010
01100001
01101001
01101110
n=1 -> 0
n=2 -> 01
n=3 -> 110
...
num >> n
num % (1 << n)
num % n
(num >> n) << n
(num << n) >> n
01100100,
01100001,
01101110,
01101011,
01110100
01110010
01100001
01101001
01101110
n=1 -> 0
n=2 -> 01
n=3 -> 110
...
A. YES
B. NO
A. YES
B. NO
A.
B.
C.
D.
E.
Sound
A.
B.
C.
D.
A.
B.
C.
D.
A.
copy_samples[(int)i].setValue(my_samples[copy_idx].getValue());
B.
copy_samples[i].setValue(my_samples[copy_idx].getValue());
C.
copy_samples[copy_idx].setValue(my_samples[i].getValue());
D.
copy_samples[copy_idx].setValue(my_samples[(int)i].getValue());
A.
copy_samples[(int)i].setValue(my_samples[copy_idx].getValue());
B.
copy_samples[i].setValue(my_samples[copy_idx].getValue());
C.
copy_samples[copy_idx].setValue(my_samples[i].getValue());
D.
copy_samples[copy_idx].setValue(my_samples[(int)i].getValue());
Sound steganography
You hide a 20x20 picture inside a sound, using the last 4 bits of each sound sample to store one
color channel (red, green, or blue). How many sound samples do you need to store the whole
picture?
sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)
(value << 4)
(value << 4)
(value >> 4)
(value >> 4)
value >> 4 +
>> 4 +
>> 4 +
<< 4 +
<< 4 +
(ch >>
ch
(ch >> 4)
(ch >> 4)
ch
4) << 4
Hint: color
values only
have 8 bits
Sound steganography
You hide a 20x20 picture inside a sound, using the last 4 bits of each sound sample to store one
color channel (red, green, or blue). How many sound samples do you need to store the whole
picture?
1200 samples
sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)
(value << 4)
(value << 4)
(value >> 4)
(value >> 4)
value >> 4 +
>> 4 +
>> 4 +
<< 4 +
<< 4 +
(ch >>
ch
(ch >> 4)
(ch >> 4)
ch
4) << 4
Hint: color
values only
have 8 bits
Sound steganography
You hide a 20x20 picture inside a sound, using the last 4 bits of each sound sample to store one
color channel (red, green, or blue). How many sound samples do you need to store the whole
picture?
1200 samples
sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)
(value << 4)
(value << 4)
(value >> 4)
(value >> 4)
value >> 4 +
>> 4 +
>> 4 +
<< 4 +
<< 4 +
(ch >>
ch
(ch >> 4)
(ch >> 4)
ch
4) << 4
Hint: color
values only
have 8 bits
Sound steganography
You hide a 20x20 picture inside a sound, using the last 4 bits of each sound sample to store one
color channel (red, green, or blue). How many sound samples do you need to store the whole
picture?
1200 samples
sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)
(value << 4)
(value << 4)
(value >> 4)
(value >> 4)
value >> 4 +
>> 4 +
>> 4 +
<< 4 +
<< 4 +
(ch >>
ch
(ch >> 4)
(ch >> 4)
ch
4) << 4
Hint: color
values only
have 8 bits