CS506 Assignment 1 solution Spring 2017

Question 1: Marks 10
Your job is to take code snippets from the pool and place them into the blank lines in the code. You
may use the same snippets more than once, and you won’t need to use all the snippets. Your goal is to
make a class that will compile and run and produce the output listed.
Output:
Code:
class Triangle {
double area;
int height;
int length;
public static void main(String [] args) {
____________
_______________________
while ( __________ ) {
__________________________
________.height = (x + 1) * 2;
________.length = x + 4;
__________________________
System.out.print(“triangle “+x+”, area”);
System.out.println(“ = “ + _______.area);
________________
}
______________
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;
}
}
Pool:
Question 2: Marks 10
Your job is to take code snippets from the pool and place them into the blank lines in the code. You
may not use the same snippets more than once, and you won’t need to use all the snippets. Your goal
is to make a class that will compile and run and produce the output listed.
Output:
Code:
public class Puzzle4 {
public static void main(String [] args) {
___________________________________
int y = 1;
int x = 0;
int result = 0;
while (x < 6) {
___________________________
___________________________
y = y * 10;
_________________
}
x = 6;
while (x > 0) {
_________________
result = result + ___________________
}
System.out.println(“result “ + result);
}
}
class ___________ {
int ivar;
________ ______ doStuff(int _________) {
if (ivar > 100) {
return _________________________
} else {
return _________________________
}
}
}
Pool

Solution :

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; } 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); } void setArea() { area = (height * length) / 2; } }
DOWNLOAD SOLUTION HERE
loading...