solutions yes

3
1. a) Integer.parseInt(cmdLn[0]) har typ int. b) a har värde 8 b har värde 3 p har värde 24 q har värde 2 r har värde 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;

Upload: r3ddevil

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

8/10/2019 Solutions yes

http://slidepdf.com/reader/full/solutions-yes 1/3

1.a)Integer.parseInt(cmdLn[0]) har typ int.b)a har värde 8b har värde 3p har värde 24q har värde 2r har värde 2

2.011235813213455

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 = " + atLeastTwoOnesInTwelve);  } 

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;

8/10/2019 Solutions yes

http://slidepdf.com/reader/full/solutions-yes 2/3

  }  }  return count;  }}

5. Här väljer jag att återge det som finns på kursbokens hemsida. Jag vill med detta föddla att det är värt att läsa den literatur som finns för varje kurs: det är ofta så ate allt diskuteras på föreläsningar (just referenstyper vs primitiva typer har vi diskuterat flera gånger!).

Java has two different categories of data types: primitive types and reference types.

The primitive types and the associated operations are typically implemented directly in hardware, so they are especially efficient. When we declare a variable of a primitive type, the system allocates enough memory to store a value of thattype (e.g., 4 bytes for an int and 8 bytes for a double). We can initialize or modify the value using the assignment operator.

Reference types have very different characteristics. A reference stores the memory 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 the 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 the reference points to nothing. Each reference points to one object, but two ormore 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 the appropriate type) or we can also use the keyword new to make it point to a newly created object.An analogy. Let the object be a house. A reference could be a paper with streetaddress of house written in pencil. Each piece of paper can have at most one address. We can give piece of paper to a house painter and tell them to paint the house red. Can have multiple pieces of paper with the same address. When the house 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 address. 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;  }}

8/10/2019 Solutions yes

http://slidepdf.com/reader/full/solutions-yes 3/3

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

}