Question

Currently I am reading Head first java book. During this, I stuck at this pool puzzle. In this puzzle I am able to produce complete output but not able to produce blank space in output(as given below).
Pool puzzle means- According to book I need to use snippet code from pool(which is draw in book) and then place them into the blank lines in the code.

So this the main code in which I need to put code snippets.
Note - Blank lines are shown by dash lines in this code.(--------)
You can use one code snippet for one blank space and you need not to use all code snippets.

class triangle{
double area;
int height;
int length;

public static void main(String[] args){
------------    //blank space********
--------------  //blank space***********
while(-------){  //blank space *********

------------------ //blank space********
-------.height=(x+1)*2;  //blank space********
-----.length=x+4;  //blank space********
-----.setarea(); //blank space******

System.out.print("tringle "+x+" , area");
System.out.println(" = "+---------.area); //blank space********
----------  //blank space********

}
--------- //blank space********
x=27;
triangle t5= ta[2];
ta[2].area=343;
System.out.print("y= " + y);
System.out.println(" , t5 area = "+t5.area);
}
void setarea(){
----------=(height*length)/2;  //blank space********
}
}

You can use these codes snippets in above given blank spaces. You can use one code snippet more then once and not need to use all code snippets. You cannot use any other code except these below given code snippets.

triangele[]ta=new triangle[4];
triangel ta = new [] triangle[4];
triangle [] ta = new triangel[4];

x
y

area
ta.area
ta.x.area
ta[x].area

4, t5 area=18.0
4, t5 area=343.0
27,t5 area =18.0
27,t5 area=343.0

ta[x] setarea();
ta.x = setarea();
ta[x].setarea();

int x;
int y;
int x=0;
int x=1;
int y=x;

x=x+1;
x=x+2;
x=x-1;

28.0
30.0

ta=new triangle();
ta[x]=new triangle();
ta.x=new triangle();

ta.x
ta(x)
ta[x]

x<4
x<5

I need to produce this output on console screen. (I am not using any IDE)
Note- In given below output (...........) dot lines are blank space means, we need to print at place of (......) these dot lines. I am not able to print this blank space and this is my problem here.

triangle 0, area = 4.0
triangle 1, area = 10.0
triangle 2, area = 18.0
triangel 3, area = ..........
y = ........................

No correct solution

OTHER TIPS

class Triangle {
double area;
int height;
int length;

public static void main (String[] args) {
int x = 0;
Triangle[] ta = new Triangle[4];
while (x < 4) {

ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();

System.out.print("triangle " +x+ ", area");
System.out.println(" = " + ta[x].area);
x = x + 1;

}//close while
int y  = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " +y);
System.out.println(", t5 area = " +t5.area);
}//close main

void setArea() {
area = (height * length) / 2;
}//close setArea()
}//close class

The code above has an output of:

triangle 0, area = 4.0
triangle 1, area = 10.0
triangle 2, area = 18.0
triangle 3, area = 28.0
y = 4, t5 area = 343.0

this is the answer , i have typed it below

class Triangle {
double area;
int height;
int length;

public static void main (String[] args) {
int x = 0;
Triangle[] ta = new Triangle[4];
while (x < 4) {

ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();

System.out.print("triangle " +x+ ", area");
System.out.println(" = " + ta[x].area);
 x = x + 1;

 }//close while
  int y  = x;
  x = 27;
  Triangle t5 = ta[2];
  ta[2].area = 343;   
   System.out.print("y = " +y); 
   System.out.println(", t5 area = " +t5.area);
   }//close main

  void setArea() {
  area = (height * length) / 2;
  }
  }

i hope it helps

class triangle{
double area;
int height;
int length;

public static void main(String[] args){
triangle [] ta = new triangel[4];
int x=0;  
while(x<4){ 

------------------ //blank space********
ta[x].height=(x+1)*2;  //blank space********
ta[x].length=x+4;  //blank space********
ta[x].setarea(); //blank space******

System.out.print("tringle "+x+" , area");
System.out.println(" = "+ ta[x].area); //blank space********
x++ 

}
int y = 28; 
x=27;
triangle t5= ta[2];
ta[2].area=343;
System.out.print("y= " + y);
System.out.println(" , t5 area = "+t5.area);
}
void setarea(){
 area =(height*length)/2;  // area should contain the area of the triangle
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top