Вы находитесь на странице: 1из 83

CSE 8A Final Review

2nd Session
Alan Kuo
Haiyu Huang

Well start off easy...

Well start off easy...

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;

while(count < 100){


sum += 1;
count += gen.nextInt(6) + 1;
}

while(count <= 100){


sum += gen.nextInt(6) + 1;
count += 1;
}

while(count < 100){


sum += gen.nextInt(6) + 1;
count += 1;
}

while(count <= 100){


sum += 1;
count += gen.nextInt(6) + 1;
}

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;

while(count < 100){


sum += 1;
count += gen.nextInt(6) + 1;
}

while(count <= 100){


sum += gen.nextInt(6) + 1;
count += 1;
}

while(count < 100){


sum += gen.nextInt(6) + 1;
count += 1;
}

while(count <= 100){


sum += 1;
count += gen.nextInt(6) + 1;
}

Which of these will make an image noticeably more green?


Picture pic=new
Picture(FileChooser.pickAFile());
Pixel[ ] pixArray=pic.getPixels();
for(Pixel p : pixArray){
??????????
}

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

Which of these will make an image noticeably more green?


Picture pic=new
Picture(FileChooser.pickAFile());
Pixel[ ] pixArray=pic.getPixels();
for(Pixel p : pixArray){
??????????
}

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

How do you invert one color in a pixel?


(that is, if you did this for all 3 colors, it would create a negative)
int value = pixel.getRed();
int inverse = ?????;

A.
B.
C.
D.
E.

-value
255 + value
255 - value
value - 255
None of these

How do you invert one color in a pixel?


(that is, if you did this for all 3 colors, it would create a negative)
int value = pixel.getRed();
int inverse = ?????;

A.
B.
C.
D.
E.

-value
255 + value
255 - value
value - 255
None of these

What do you expect to happen here?


Picture pic = new Picture(FileChooser.pickAFile());
for (double x = 0; x < pic.getWidth(); x += 0.99) {
for (double y = 0; y < pic.getHeight(); y++) {
pic.getPixel((int)x, (int)y).setColor(Color.BLACK);
}
}
pic.show();

A.
B.
C.
D.

The whole picture turns black


Compile error
Infinite loop at runtime
Out-of-bounds error

What do you expect to happen here?


Picture pic = new Picture(FileChooser.pickAFile());
for (double x = 0; x < pic.getWidth(); x += 0.99) {
for (double y = 0; y < pic.getHeight(); y++) {
pic.getPixel((int)x, (int)y).setColor(Color.BLACK);
}
}
pic.show();

A.
B.
C.
D.

The whole picture turns black


Compile error
Infinite loop at runtime
Out-of-bounds error

What does this do?


Picture pic = new Picture("rainbow dash.png");
for (int x = 0; x < pic.getWidth() / 2; x++) {
for (int y = 0; y < pic.getHeight(); y++) {
Pixel one_side = pic.getPixel(x,y);
Pixel other_side = pic.getPixel(pic.getWidth() - x - 1, y);
one_side.setColor(other_side.getColor());
}
}
pic.show();

A.
B.
C.
D.
E.

Mirror right into left


Mirror left into right
Flip around the horizontal
Flip around the vertical
None of these

What does this do?


Picture pic = new Picture("rainbow dash.png");
for (int x = 0; x < pic.getWidth() / 2; x++) {
for (int y = 0; y < pic.getHeight(); y++) {
Pixel one_side = pic.getPixel(x,y);
Pixel other_side = pic.getPixel(pic.getWidth() - x - 1, y);
one_side.setColor(other_side.getColor());
}
}
pic.show();

A.
B.
C.
D.
E.

Mirror right into left


Mirror left into right
Flip around the horizontal
Flip around the vertical
None of these

What if statement should we use so this draws a grid?


In Picture.java:
public void make_grid() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
if (___________________________)
getPixel(x, y).setColor(Color.BLACK);
}
}
}

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

What if statement should we use so this draws a grid?


In Picture.java:
public void make_grid() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
if (___________________________)
getPixel(x, y).setColor(Color.BLACK);
}
}
}

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

Class & Constructors

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Somewhere else in the same class in


Banana.java:

Somewhere else in Main.java:

Banana b = new Banana();


int d = b.daylight;

Banana b = new Banana();


int d = b.daylight;

How about now?


Does the indicated line cause a
compile error?
A. Yes
B. No

A. Yes
B. No

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Somewhere else in the same class in


Banana.java:

Somewhere else in Main.java:

Banana b = new Banana();


int d = b.daylight;

Banana b = new Banana();


int d = b.daylight;

How about now?


Does the indicated line cause a
compile error?
A. Yes
B. No

A. Yes
B. No

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Somewhere else in the same class in


Banana.java:

Somewhere else in Main.java:

Banana b = new Banana();


int d = b.daylight;

Banana b = new Banana();


int d = b.daylight;

How about now?


Does the indicated line cause a
compile error?
A. Yes
B. No

A. Yes
B. No

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Consider this class in Banana.java:


public class Banana {
private int daylight;
private int come;
}

Somewhere else in the same class in


Banana.java:

Somewhere else in Main.java:

Banana b = new Banana();


int d = b.daylight;

Banana b = new Banana();


int d = b.daylight;

How about now?


Does the indicated line cause a
compile error?
A. Yes
B. No

A. Yes
B. No

What happens when you dont initialize a Member


Variable that you declared?
public class foo{
private Integer x;
private String str;
private int z;
}
Note that member variables can also
be called member fields
Nonstatic member variables can also
be called instance variables
Static member variables can be called
class variables

A.

Compile error

B.

Runtime error if you ever try to use them


in your methods without initializing them
first somewhere (like a constructor)

C.

They take on some default value,


depending on the variable type

D.

This is terrible practice

What happens when you dont initialize a member


variable that you declared?
public class foo{
private Integer x; //null
private String str; //null
private int z;
//0
}

A.

Compile error

B.

Runtime error if you ever try to use them


in your methods without initializing them
first somewhere (like a constructor)

C.

They take on some default value,


depending on the variable type

D.

This is terrible practice

Typically, Objects default to null,


and primitives default to 0.

What happens when you dont initialize a local


variable that you declared?
A.
public class foo{
B.
public void bar(){
int x;
System.out.println(x);
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

What happens when you dont initialize a local


variable that you declared?
A.
public class foo{
B.
public void bar(){
int x;
System.out.println(x);
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

1 public class Pony {


2
private int x;
3
private int y;
4
private Picture picture;
5
6
public Pony(int x, int y) {
7
this.x = x;
8
this.y = y;
9
}
10
11
public Picture getPicture() {
12
return picture;
A.
13
}
14
15
public static void main(String[] args) {
16
Pony rarity = new Pony(0, 0);
B.
17
Picture p = rarity.picture;
C.
18
rarity.getPicture().show();
D.
19
}
20 }

Which line causes an error?


A.
B.
C.
D. (didnt initialize picture)
E. None of these or no error

1 public class Pony {


2
private int x;
3
private int y;
4
private Picture picture;
5
6
public Pony(int x, int y) {
7
this.x = x;
8
this.y = y;
9
}
10
11
public Picture getPicture() {
12
return picture;
A.
13
}
14
15
public static void main(String[] args) {
16
Pony rarity = new Pony(0, 0);
B.
17
Picture p = rarity.picture;
C.
18
rarity.getPicture().show();
D.
19
}
20 }

Which line causes an error?


A.
B.
C.
D. (didnt initialize picture)
E. None of these or no error

Coding exercise: make a class


Class is called Dog
It has 2 instance variables:
age (integer)
name (String)

Make a constructor which initializes everything


Make getters and setters (2 getters, 2 setters)

Coding exercise: make a class


Class is called Dog

public class Dog{


private int age;
private String name;
public Dog(int age, String name) {
this.age = age;
this.name = new String(name);
}
public void setAge(int age) {
this.age = age;
}
public void setName(String newName) {
//this is implicit
name = new String(newName);
}
public int getAge() {
return this.age;
}
public String getName(){
return this.name;
}

It has 2 instance variables:


age (integer)
name (String)

Make a constructor which initializes everything


Make getters and setters (2 getters, 2 setters)

Memory Models

Heap Vs. Stack

Concepts:

Each object created is stored in heap section


Each function has its own Stack frame
References/Variable Names are stored in Stack

Heap

Enable to modify objects when passing reference into parameter of


methods (Parameter reference would point to object)

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);
}

Whats the result?


A) Compile error
B) Runtime error
C) 51
50
D) 50
50
E) something else

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);
}

Whats the result?


A) Compile error
B) Runtime error
C) 51
50
D) 50
50
E) something else

public static void silly(int a, int b) {

public static void silly(int a, int b) {

public static void silly(int a, int b) {

public static void silly(int a, int b) {

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Heap

Stack
circle1

radius
color

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Heap

Stack
circle1

radius
color

temp

5 10

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Heap

Stack
radius

circle1

color

temp

radius

10

5 10

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Heap

Stack
radius

circle1

color

temp

radius

10 12

5 10 12

Memory (heap & stack)


Import java.awt.*;
public class Circle {
private int radius;
private Color color;
public Circle(int r, Color c){
radius = r;
color = c;
}
public int getRadius(){ return radius;}
public Color getColor(){ return color;}
public void setRadius(int r){ radius = r; }
public void setColor(Color c){ color = c; }

public static void main (String[] args) {


Circle circle1 = new Circle ( 5, new Color(255, 255, 255) );
Circle temp = circle1;
temp.setRadius( 10 );

int radius = circle1.getRadius();


radius += 2;
temp.setRadius( radius );
temp = new Circle ( 1, new Color(0, 0, 0) );
}

Heap

Stack

radius 5 10

circle1

color

temp
radius
radius

5 12

color

12

public class Cat {


private int x;
private int y;
}
public class Box {
private Cat tom;
public Cat getTom() {
return tom;
}
}

This memory model is supposed to represent what is


going on with the variable box in Main.
Does this memory model look right? If not, what can we
change to make it correct?
A.
B.
C.
D.

No: we need a constructor in class Box setting up tom


No: tom should not point at anything in the memory model
A or B
Yes, its correct

Box (in Main)


public class Main {
public static void main(String[] args) {
Box box = new Box();
}
}

tom

Cat
x
y

0
0

public class Cat {


private int x;
private int y;
}
public class Box {
private Cat tom;
public Cat getTom() {
return tom;
}
}

This memory model is supposed to represent what is


going on with the variable box in Main.
Does this memory model look right? If not, what can we
change to make it correct?
A.
B.
C.
D.

No: we need a constructor in class Box setting up tom


No: tom should not point at anything in the memory model
A or B
Yes, its correct

public class Main {


public static void main(String[] args) {
Box box = new Box();
}
}

Box (in Main)


tom

Cat
x
y

0
0

Consider this class:


public class Person{
private int age;
private Picture picture;
private String name;
//make the constructor
An example person

Lets make a constructor for our class Person. What could the header look like?
A.
B.
C.
D.

public
public
public
public

Person new(int age, Picture picture, String name)


void Person()
Person(int age, Picture picture, String name)
void Person(int age, Picture picture, String name)

Consider this class:


public class Person{
private int age;
private Picture picture;
private String name;
//make the constructor
An example person

Lets make a constructor for our class Person. What could the header look like?
A.
B.
C.
D.

public
public
public
public

Person new(int age, Picture picture, String name)


void Person()
Person(int age, Picture picture, String name)
void Person(int age, Picture picture, String name)

public class Person{


private int age;
private Picture picture;
private String name;
public Person(int age, Picture picture, String name) {
//TODO
}
}

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

public class Person{


private int age;
private Picture picture;
private String name;
public Person(int age, Picture picture, String name) {
//TODO
}
}

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

Whats the right way to extract the lower n bits of a


number?
A.
B.
C.
D.
E.

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
...

Whats the right way to extract the lower n bits of a


number?
A.
B.
C.
D.
E.

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
...

Whats the right way to extract the lower n


bits of a number?
Would this work?
num % (2^n)

A. YES
B. NO

Whats the right way to extract the lower n


bits of a number?
Would this work?
num % (2^n)

A. YES
B. NO

Remember that 2^n in java means:


2 XOR n
This is NOT the same as Math.pow(2,n) or (1<<n), which you could use

How would you extract the top n bits of an integer?


Note that integers are always 32 bits long

A.
B.
C.
D.
E.

(num << n) >> 32


num % (32 << n)
(num >> 32) << n
num % 1 << (32 - n)
num >> (32 - n)

01101000011000010110100001100001 -> 01101

How would you extract the top n bits of an integer?


A.
B.
C.
D.
E.

(num << n) >> 32


num % (32 << n)
(num >> 32) << n
num % 1 << (32 - n)
num >> (32 - n)

01101000011000010110100001100001 -> 01101

Consider this expression. You dont know the value of num:


(num << 4) % 8
What is this equivalent to?
A.
B.
C.
D.

(num >> 3) << 3


0
num << 1
No way to know

Consider this expression. You dont know the value of num:


(num << 4) % 8
What is this equivalent to?
A.
B.
C.
D.

(num >> 3) << 3


0
num << 1
No way to know

Sound

What does this do?


public Sound something() {
Sound copy = new Sound((int)(this.getNumSamples() * 2));
SoundSample[] copy_samples = copy.getSamples();
SoundSample[] my_samples = this.getSamples();
for (int i = 0; i < this.getNumSamples() * 2; i++) {
copy_samples[i].setValue(my_samples[i / 2].getValue());
}
return copy;
}

A.
B.
C.
D.

Returns a version of the calling Sound object but 2x slower


Returns a version of the calling Sound object but 2x faster
Returns a copy of the calling object
Nothing

What does this do?


public Sound something() {
Sound copy = new Sound((int)(this.getNumSamples() * 2));
SoundSample[] copy_samples = copy.getSamples();
SoundSample[] my_samples = this.getSamples();
for (int i = 0; i < this.getNumSamples() * 2; i++) {
copy_samples[i].setValue(my_samples[i / 2].getValue());
}
return copy;
}

A.
B.
C.
D.

Returns a version of the calling Sound object but 2x slower


Returns a version of the calling Sound object but 2x faster
Returns a copy of the calling object
Nothing

Coding exercise: modify this so it makes the calling


object 1.5 times FASTER (and returns a copy)
public Sound something() {
Sound copy = new Sound(______________________);
SoundSample[] copy_samples = copy.getSamples();
SoundSample[] my_samples = this.getSamples();

//Number of samples in the result

//Make your for-loop here


//You might need an extra variable
return copy;
}
Hints:
How many samples are going to be in the copy?
Which sound needs to have all of its samples set to a value?
In which sound are we going to skip samples to make it sound faster?
You did something like this in the last lab
(there was a double involved in the loop)

Make a sound 1.5x faster (in Sound.java)


public Sound something() {
Sound copy = new Sound((int)(this.getNumSamples() / 1.5));
SoundSample[] copy_samples = copy.getSamples();
SoundSample[] my_samples = this.getSamples();
int copy_idx = 0;
for (double i = 0; i < this.getLength(); i += 1.5) {
????;
copy_idx++;
}
return copy;
}
What goes in the blank?

//Number of samples in the result

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());

Make a sound 1.5x faster (in Sound.java)


public Sound something() {
Sound copy = new Sound((int)(this.getNumSamples() / 1.5));
SoundSample[] copy_samples = copy.getSamples();
SoundSample[] my_samples = this.getSamples();
int copy_idx = 0;
for (double i = 0; i < this.getLength(); i += 1.5) {
????;
copy_idx++;
}
return copy;
}

//Number of samples in the result

What goes in the blank?

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?

Let each sound sample store one color channel


(red, green, or blue). How do you retrieve one
color channel out of a sound sample?
A.
B.
C.
D.
E.

sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)

How do you put one color channel IN a sound sample?


int value = sample.getValue();
int ch = ...some color channel...
Enter what the new sample value should be:
A.
B.
C.
D.
E.

(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

Let each sound sample store one color channel


(red, green, or blue). How do you retrieve one
color channel out of a sound sample?
A.
B.
C.
D.
E.

sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)

How do you put one color channel IN a sound sample?


int value = sample.getValue();
int ch = ...some color channel...
Enter what the new sample value should be:
A.
B.
C.
D.
E.

(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

Let each sound sample store one color channel


(red, green, or blue). How do you retrieve one
color channel out of a sound sample?
A.
B.
C.
D.
E.

sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)

How do you put one color channel IN a sound sample?


int value = sample.getValue();
int ch = ...some color channel...
Enter what the new sample value should be:
A.
B.
C.
D.
E.

(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

Let each sound sample store one color channel


(red, green, or blue). How do you retrieve one
color channel out of a sound sample?
A.
B.
C.
D.
E.

sample.getValue() >> 4
sample.value % 16
sample.getValue() % 4
(sample.getValue() >> 4) << 4
sample.getValue() % (1<<4)

How do you put one color channel IN a sound sample?


int value = sample.getValue();
int ch = ...some color channel...
Enter what the new sample value should be:
A.
B.
C.
D.
E.

(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

Any other Questions?


Good Luck on Final!!

Вам также может понравиться