You are on page 1of 3

1.

a)
Integer.parseInt(cmdLn[0]) har typ int.
b)
a har vrde 8
b har vrde 3
p har vrde 24
q har vrde 2
r har vrde 2
2.
0
1
1
2
3
5
8
13
21
34
55
3.
public static double[] stats(double[] a){
double res[] = new double[3];
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double sum = 0;
for(double e : a){
if(e < min){min = e;}
if(e > max){max = e;}
sum = sum + e;
}
res[0] = min;
res[1] = max;
res[2] = sum / a.length;
return res;
}
4.
public class Four{
public static void main(String[] cmdLn){
int N = Integer.parseInt(cmdLn[0]);
int atLeastOneOneInSix = 0;
int atLeastTwoOnesInTwelve = 0;
for(int i = 0; i < N; i++){
if(countOnesIn(6)>=1){atLeastOneOneInSix++;}
if(countOnesIn(12)>=2){atLeastTwoOnesInTwelve++;}
}
System.out.println("At least one One in six = " + atLeastOneOneInSix);
System.out.println("At least two Ones in twelve = " + atLeastTwoOnesInTw
elve);
}
private static int countOnesIn(int bound){
int count = 0;
for(int i = 0; i < bound; i++){
if((int)(Math.random()*6) + 1 == 1){
count = count + 1;

}
}
return count;
}
}
5. Hr vljer jag att terge det som finns p kursbokens hemsida. Jag vill med detta frme
ddla att det r vrt att lsa den literatur som finns fr varje kurs: det r ofta s att in
te allt diskuteras p frelsningar (just referenstyper vs primitiva typer har vi disk
uterat flera gnger!).
Java has two different categories of data types: primitive types and reference t
ypes.
The primitive types and the associated operations are typically implemented dire
ctly in hardware, so they are especially efficient. When we declare a variable o
f a primitive type, the system allocates enough memory to store a value of that
type (e.g., 4 bytes for an int and 8 bytes for a double). We can initialize or m
odify the value using the assignment operator.
Reference types have very different characteristics. A reference stores the memo
ry address of an object. It captures the difference between a thing and its name
. We say that the reference points to the object and often draw an arrow from th
e reference to what it points to. A reference of a given type always points to a
n object of the correct type or to the special value null which indicates that t
he reference points to nothing. Each reference points to one object, but two or
more references can point to the same object. We initialize a reference by using
an assignment statement. We can either set it equal to another reference (of th
e appropriate type) or we can also use the keyword new to make it point to a new
ly created object.
An analogy. Let the object be a house. A reference could be a paper with street
address of house written in pencil. Each piece of paper can have at most one add
ress. We can give piece of paper to a house painter and tell them to paint the h
ouse red. Can have multiple pieces of paper with the same address. When the hous
e is painted, both pieces of paper have the street address of the same red house
. It's possible to erase what's on the paper, and write down a new street addres
s. But if you change what's written on your piece paper, it doesn't change what'
s written on my piece of paper.
6.
public class Six{
public static void main(String[] cmdLn){
Picture pic = new Picture(cmdLn[0]);
Picture coldPic = noRed(pic);
coldPic.save("noRed" + cmdLn[0]);
pic.show();
coldPic.show();
}
private static Picture noRed(Picture p){
Picture cold = new Picture(p.width(), p.height());
for(int x = 0; x < p.width(); x++){
for(int y = 0; y < p.height(); y++){
java.awt.Color c = p.get(x,y);
cold.set(x,y,new java.awt.Color(0,c.getGreen(),c.getBlue()));
}
}
return cold;
}
}

7.
public class Reply {
private int bulls, cows;
public Reply(int bulls, int cows) {
this.bulls = bulls;
this.cows = cows;
}
public int getBulls() {return bulls;}
public int getCows() {return cows;}
public boolean equals(Reply other) {
return bulls == other.bulls && cows == other.cows;
}
public String toString() {
return bulls + " " + cows;
}
}

You might also like