Β
| 1. | class A{ |
| 2. | Β public int temp = 4; |
| 3. | Β public int sum = 1; |
| 4. | Β public int y = 2; |
| 5. | Β public A(){ |
| 6. | Β Β Β y = temp β 2; |
| 7. | Β Β Β sum = temp + 3; |
| 8. | Β Β Β temp-=2; |
| 9. | Β } |
| 10. | Β public void methodA(int m, int n){ |
| 11. | Β Β Β int x = 0; |
| 12. | Β Β Β y = y + m + (temp++); |
| 13. | Β Β Β x = x + 2 +Β n; |
| 14. | Β Β Β sum = sum + x + y; |
| 15. | Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| 16. | Β } |
| 17. | } |
| 18. | class B extends A { |
| 19. | Β public int x = 1; |
| 20. | Β public int sum = 2; |
| 21. | Β public B(){ |
| 22. | Β Β Β y = temp + 3 ; |
| 23. | Β Β Β sum = 3 + temp + 2; |
| 24. | Β Β Β temp-=1; |
| 25. | Β }Β Β |
| 26. | Β public B(B b){ |
| 27. | Β Β Β sum = b.sum; |
| 28. | Β Β Β x = b.x; |
| 29. | Β } |
| 30. | Β public void methodB(int m, int n){Β Β Β Β |
| 31. | Β Β Β intΒ y =0; |
| 32. | Β Β Β y = y + this.y;Β |
| 33. | Β Β Β x = this.y + 2 + temp; |
| 34. | Β Β Β methodA(x, y); |
| 35. | Β Β Β sum = x + y + super.sum; |
| 36. | Β Β Β System.out.println(x + β β + y+ β β + sum); |
| 37. | Β } |
| 38. | } |
Β
Β Β Β Consider the following code: [Answer on the Question Paper]
| AΒ Β Β Β Β Β Β a1 = new A();
BΒ Β Β Β Β Β Β b1 = new B(); B b2 = new B(b1); a1.methodA(1, 1); b1.methodA(1, 2); b2.methodB(3, 2); |
x | Y | Sum |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β |
Β
Β
Given the following classes, write the code for the BBAStudent class so that the following output is printed when we run the TestStudent class.
Β
Name : Default BBA Student Department: BBA
Name : Humty Dumty Department: BBA
Name : Little Bo Peep Department: BBA Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β
Β
| public class Student{ |
| Β private String name = βJust a Studentβ;Β Β |
| Β private String department = βnothingβ; |
| Β public void setDepartment(String dpt){ |
| Β Β Β this.department = dpt; |
| Β } |
| Β protected String getName(){ |
| Β Β Β return name; |
| Β } |
| Β protected void setName(String name){ |
| Β Β Β this.name = name; |
| Β } |
| Β public String toString(){ |
| Β Β Β return βName : β + name + β Department: β + department; |
| Β } |
| } |
| public class TestStudent{ |
| Β public static void printName(Student s){ |
| Β Β Β System.out.println(s.toString()); |
| Β } |
| Β public static void main(String [] args){ |
| Β Β Β printName(new BBAStudent()); |
| Β Β Β printName(new BBAStudent(βHumty Dumtyβ)); |
| Β Β Β printName(new BBAStudent(βLittle Bo Peepβ));Β Β Β Β |
| Β } |
| } |
Β
Β
Β
Β [??????????????????????????????????????????]
Write a java program, which calculates βthe area of a circleβ and βthe volume of a sphereβ by overriding the space() method in the subclass. [Extend the following super class βPointβ with necessary overriding of specific method. DO NOT CHANGE THE POINT CLASS]. Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β class Point {Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β private double radius;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Point ( double r) {
radius = r;
}
double space ( ) {
System.out.println(βSpace for a Point canβt be definedβ);
return 0;
}
protected double getRadius(){
return radius;
}
}
// create new sub class
Β
Implement the above program to include constructors for all the subclasses & use βsuper( )β to call super-class constructors, if necessary.
Β
Sample Input/Output Enter radius of Circle: 5 Creating a Circle β¦ done!
The area of the Circle is 78.539816339744830961566084581988
Β
Enter radius of Sphere: 7 Creating a Sphere β¦ done!
The area of the Sphere is 205.25072003453315824622603437426
Β
Β
Β
Β
Β
Β
Design a βVehicleβ class. A vehicle assumes that the whole world is a 2 dimensional graph paper. It maintains its x and y coordinates (both are integers). The vehicle gets manufactured (constructed?) at (0,0) coordinate.
Β
Write a user class called βVehicleβ. It must have methods to move up, down, left, right and a toString method for printing current coordinate.Β
Β
Note: All moves are 1 step. That means a single call to any move method changes value of either x or y or both by 1.
Β
Take help from:Β http://www.javabeginner.com/learn-java/java-tostring-method http://cscie160-distance.com/toString.html
Β
public class VehicleUser{Β Β Β Β public static void main(String[] args){
Vehicle car = new Vehicle();Β Β Β Β Β Β Β Β System.out.println(car.toString());Β Β Β Β Β Β Β Β car.moveUp();
System.out.println(car.toString());Β Β Β Β Β Β Β Β car.moveLeft();
System.out.println(car.toString());Β Β Β Β Β Β Β Β car.moveDown();
System.out.println(car.toString());Β Β Β Β Β Β Β Β car.moveRight();
Β
| Β
// see, output for following two lines are same because toString() is automatically called. So, you can omit toString when printing.Β Β Β Β Β Β Β Β System.out.println(car.toString()); System.out.println(car); } } |
| Expected Output:Β
(0, 0) (0, 1) (-1, 1) (-1, 0) (0, 0) (0, 0) |
Β
Β
Task 5
Β
Design a Vehicle2010 class which inherits movement methods from Task1 and adds new methods called move UpperRight, UpperLeft, LowerRight, LowerLeft. Each of these diagonal move methods must re use two inherited and appropriate move methods. Write user class as well which will show that all of your methods are working. A small user class is shown below as an example.
Β
Note: All moves are 1 step. That means a single call to any move method changes value of either x or y or both by 1.Β
Β
Additionally, you have to write an βequalsβ method which tests if significant class properties are same (in this case x and y).
Take help on βequalsβ method from:
- http://www.ibiblio.org/java/course/week4/37.html
- http://www.javaworld.com/javaworld/jw-06-2004/jw-0614-equals.html
- http://www.artima.com/lejava/articles/equality.html
Β
| public class Vehicle2010User{Β Β Β Β public static void main(String[] args){
Vehicle2010 car = new Vehicle2010();Β Β Β Β Β Β Β Β System.out.println(car);Β Β Β Β Β Β Β Β car.moveLowerLeft(); System.out.println(car); Β Vehicle2010 car2 = new Vehicle2010();Β Β Β Β Β Β Β Β car2.moveLeft(); System.out.println(car.equals(car2));Β Β Β Β Β Β Β Β car2.moveDown(); System.out.println(car.equals(car2)); } } |
| Expected Output:
(0, 0) (-1, -1) false true |
Β
Β
Β
Β
Task 6
Write the ComplexNumber class so that the following code generates the output below:
| public class Tester { |
| Β Β Β public static void main(String[] args) { |
| Β Β Β Β Β Β Β RealNumber rn = new ComplexNumber(); |
| Β Β Β Β Β Β Β System.out.println(rn); |
| Β Β Β Β Β Β Β Β |
| Β Β Β Β Β Β Β System.out.println(βββββββββ); |
| Β Β Β Β Β Β Β Β |
| Β Β Β Β Β Β Β rn = new ComplexNumber(5, 7); |
| Β Β Β Β Β Β Β System.out.println(rn); |
| Β Β Β Β Β Β Β Β |
| Β Β Β Β Β Β Β System.out.println(βββββββββ); |
| Β Β Β Β Β Β Β ComplexNumber cn = new ComplexNumber(); |
| Β Β Β Β Β Β Β cn.check(); |
| Β Β Β } |
| } |
| public class RealNumber { |
| Β Β Β private double realValue; |
| Β Β Β public double getRealValue() { |
| Β Β Β Β Β Β Β return realValue; |
| Β Β Β } |
| Β Β Β public void setRealValue(double r) { |
| Β Β Β Β Β Β Β realValue = r; |
| Β Β Β } |
| Β Β Β public RealNumber() { |
| Β Β Β Β Β Β Β this(0); |
| Β Β Β } |
| Β Β Β public RealNumber(double r) { |
| Β Β Β Β Β Β Β setRealValue(r); |
| Β Β Β } |
| Β Β Β public String toString() { |
| Β Β Β Β Β Β Β return βRealPart: β+getRealValue(); |
| Β Β Β } |
| Β Β Β public void ping() { |
| Β Β Β Β Β Β Β System.out.println(βIβm in RealNumber classβ); |
| Β Β Β } |
| } |
Β Β Β Β Β Β Β Β Β Β Β
RealPart: 1.0
ImaginaryPart: 1.0
βββββββ
RealPart: 5.0
ImaginaryPart: 7.0
βββββββ
Iβm in ComplexNumber class
Iβm in RealNumber class
Checking ended.
Β
Task 7
Write the Mango and the Jackfruit classes so that the following code generates the output below:
Β
| public class Test{Β Β |
| Β Β Β public static void testFruit(Fruit f){ |
Β
| Β Β Β Β Β Β Β System.out.println(ββ-Printing Detailβββ); |
| Β Β Β Β Β Β Β if(f.hasFormalin()){Β Β Β Β Β Β |
| Β Β Β Β Β Β Β Β Β Β Β System.out.println(βDo not eat the β+f.getName()+β.β); |
| Β Β Β Β Β Β Β Β Β Β Β System.out.println(f); |
| Β Β Β Β Β Β Β }else{ |
| Β Β Β Β Β Β Β Β Β Β Β System.out.println(βEat the β+f.getName()+β.β); |
| Β Β Β Β Β Β Β Β Β Β Β System.out.println(f); |
| Β Β Β Β Β Β Β } |
| Β Β Β }Β Β |
| Β Β Β public static void main(String [] args){ |
| Β Β Β Β Β Β Β Mango m = new Mango(); |
| Β Β Β Β Β Β Β testFruit(m); |
| Β Β Β Β Β Β Β Jackfruit j = new Jackfruit(); |
| Β Β Β Β Β Β Β testFruit(j); |
| Β Β Β } |
| } |
| public class Fruit{ |
| Β Β Β private boolean formalin = false; |
| Β Β Β public String name = ββ; |
| Β Β Β public Fruit(boolean formalin, String name){ |
| Β Β Β Β Β Β Β this.formalin = formalin; |
| Β Β Β Β Β Β Β this.name = name;Β Β Β Β |
| Β Β Β }Β Β Β Β |
| Β Β Β public String getName(){ |
| Β Β Β Β Β Β Β return name; |
| Β Β Β } |
| Β Β Β public boolean hasFormalin(){ |
| Β Β Β Β Β Β Β return formalin; |
| Β Β Β } |
| } |
Β
β-Printing Detailββ Do not eat the Mango.
Mangos are bad for you
β-Printing Detailββ Eat the Jackfruit.
Jackfruits are good for you
Task 8
Write the CheckingAccount class so that the following code generates the output below
Β
| public class Account{ |
| Β protected double balance = 0.0; |
| Β public Account(double balance){ |
| Β Β Β this.balance = balance; |
| Β } |
| Β public double getBalance(){ |
| Β Β Β return balance; |
| Β } |
| } |
| public class TestAccount{ |
| Β public static void printBalance(Account a){ |
| Β Β Β System.out.println(βAccount Balance: β + a.getBalance()); |
| Β } |
| Β public static void main(String [] args) |
| Β { |
| Β Β Β System.out.println(βNumber of Checking Accounts: β +
CheckingAccount.numberOfAccount); |
| Β Β Β printBalance(new CheckingAccount()); |
| Β Β Β printBalance(new CheckingAccount(100.00)); |
| Β Β Β printBalance(new CheckingAccount(200.00)); |
| Β Β Β System.out.println(βNumber of Checking Accounts: β +
CheckingAccount.numberOfAccount); |
| Β } |
| } |
Β
Β
Number of Checking Accounts: 0
Account Balance: 0.0
Account Balance: 100.0
Account Balance: 200.0
Number of Checking Accounts: 3
Β
Task 9
Β
Write the CSEStudent and CSE111Student class so that the following code generates the output below [
| public class Student{ |
| Β public String msg = βI love BUβ; |
| Β public String shout(){ |
| Β Β Β return msg; |
| Β } |
| } |
| public class TestStudent{ |
| Β public static void printShout(Student s){ |
| Β Β Β System.out.println(ββββββββ); |
| Β Β Β System.out.println(s.msg); |
| Β Β Β System.out.println(s.shout()); |
| Β } |
| Β public static void main(String [] args){ |
| Β Β Β Student s = new Student(); |
| Β Β Β CSEStudent cs = new CSEStudent(); |
| Β Β Β CSE111Student cs111 = new CSE111Student(); |
| Β Β Β System.out.println(s.msg); |
| Β Β Β System.out.println(cs.msg); |
| Β Β Β System.out.println(cs111.msg); |
| Β Β Β printShout(s); |
| Β Β Β printShout(cs); |
| Β Β Β printShout(cs111); |
| Β } |
| } |
Β
| Output |
| I love BU
I want to transfer to CSE I love Java Programming ββββββ I love BU I love BU |
ββββββ
I love BU
I want to transfer to CSE
ββββββ
I love BU
I love Java Programming
Β
Β
Task 10
Write the Car class so that the following code generates the output bellow
Β public class TestCars{Β Β public static void printCarDetail(Car c){
Β Β Β System.out.println(βYear: β+ c.getYear());
Β Β Β System.out.println(βTotal Number of Cars: β+ c.getObjectCount());
Β }Β Β public static void main(String [] args){
Β Β Β System.out.println(βTotal Number of Cars: β+
Car.getObjectCount());
Β Β Β System.out.println(β==============================β);Β Β Β Β Car c1 = new Car(2000);Β Β Β Β printCarDetail(c1);Β Β Β Β Car c2 = new Car(2006);Β Β Β Β printCarDetail(c2);Β Β Β Β Car c3 = new Car(2002);Β Β Β Β printCarDetail(c3);
Β Β Β System.out.println(β==============================β);Β Β Β Β System.out.println(βTotal Number of Cars: β+
Car.getObjectCount());Β Β Β Β Β Β Β Β
Β }
}
Β
Total Number of Cars: 0
==============================
Year: 2000
Total Number of Cars: 1 Year: 2006
Total Number of Cars: 2 Year: 2002
Total Number of Cars: 3
==============================
Total Number of Cars: 3
Β
Task 11
Β
Given the following classes, write the code for the Dog and the Cat class so that the following output is printed when we run the AnimalDriver class.
Animal do not make sound meow bark
Β public class Animal {
Β
Β //Name of the Animal
Β private String sound = βAnimal Soundβ;
Β
Β //Default ConstructorΒ Β public Animal(){
Β }
Β
Β //Overloaded ConstructorΒ Β Animal(String _sound){Β Β Β Β this.sound = _sound;
Β }
Β
Β //Return soundΒ Β public String makeSound(){Β Β Β Β return sound;
Β }
}Β public class AnimaDriver{
Β Β public static void printSound(Animal a){
Β Β Β System.out.println(a.makeSound());
Β }Β Β Β public static void main(String [] args){
Β Β Β Dog d1 = new Dog(βbarkβ);
Β Β Β Cat c1 = new Cat(βmeowβ);
Β Β Β Animal a1 = new Animal(βAnimal do not make soundβ);Β Β Β Β printSound(a1);Β Β Β Β printSound(c1);Β Β Β Β printSound(d1);
Β }
}
Task 12
| class A{ |
| Β public static int temp = 4; |
| Β public int sum; |
| Β public int y; |
| Β public A(){ |
| Β Β Β y = temp β 2; |
| Β Β Β sum = temp + 1; |
| Β Β Β temp-=2; |
| Β } |
| Β public void methodA(int m, int n){ |
| Β Β Β int x = 0; |
| Β Β Β y = y + m + (temp++); |
| Β Β Β x = x + 1 +Β n; |
| Β Β Β sum = sum + x + y; |
| Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| Β } |
| } |
| class B extends A { |
| Β public static int x; |
| Β public int sum; |
| Β public B(){ |
| Β Β Β y = temp + 3 ; |
| Β Β Β sum = 3 + temp + 2; |
| Β Β Β temp-=2; |
| Β }Β Β |
| Β public B(B b){ |
| Β Β Β sum = b.sum; |
| Β Β Β x = b.x; |
| Β Β Β b.methodB(2,3); |
| Β } |
| Β public void methodB(int m, int n){Β Β Β Β |
| Β Β Β intΒ y = 0; |
| Β Β Β y = y + this.y;Β |
| Β Β Β x = this.y + 2 + temp; |
| Β Β Β methodA(x, y); |
| Β Β Β sum = x + y + sum; |
| Β Β Β System.out.println(x + β β + y+ β β + sum); |
| Β } |
| } |
Consider the following code:
| AΒ Β Β Β Β Β Β a1 = new A();
BΒ Β Β Β Β Β Β b1 = new B(); B b2 = new B(b1); b1.methodA(1, 2); b2.methodB(3, 2); |
x | y | sum |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β |
Β
Task 13
| class A{ |
| Β public int temp = 4; |
| Β public int sum; |
| Β public int y; |
| Β public A(){ |
| Β Β Β y = temp β 2; |
| Β Β Β sum = temp + 3; |
| Β Β Β temp-=2; |
| Β } |
| Β public void methodA(int m, int n){ |
| Β Β Β int x = 0; |
| Β Β Β y = y + m + (temp++); |
| Β Β Β x = x + 2 +Β n; |
| Β Β Β sum = sum + x + y; |
| Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| Β } |
| } |
| class B extends A { |
| Β public int x; |
| Β public int sum; |
| Β public B(){ |
| Β Β Β y = temp + 3 ; |
| Β Β Β sum = 3 + temp + 2; |
| Β Β Β temp-=1; |
| Β }Β Β |
| Β public B(B b){ |
| Β Β Β sum = b.sum; |
| Β Β Β x = b.x; |
| Β } |
| Β public void methodB(int m, int n){Β Β Β Β |
| Β Β Β intΒ y =0; |
| Β Β Β y = y + this.y;Β |
| Β Β Β x = this.y + 2 + temp; |
| Β Β Β methodA(x, y); |
| Β Β Β sum = x + y + sum; |
| Β Β Β System.out.println(x + β β + y+ β β + sum); |
| Β } |
| } |
Β
Consider the following code:
| AΒ Β Β Β Β Β Β a1 = new A();
BΒ Β Β Β Β Β Β b1 = new B(); B b2 = new B(b1); a1.methodA(1, 1); b1.methodA(1, 2); b2.methodB(3, 2); |
x | y | sum |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β |
Task 14
| 1 | class A{ |
| 2 | Β public int temp = 4; |
| 3 | Β public int sum = 1; |
| 4 | Β public int y = 2; |
| 5 | Β public A(){ |
| 6 | Β Β Β y = temp β 2; |
| 7 | Β Β Β sum = temp + 3; |
| 8 | Β Β Β temp-=2; |
| 9 | Β } |
| 10 | Β public void methodA(int m, int n){ |
| 11 | Β Β Β int x = 0; |
| 12 | Β Β Β y = y + m + (temp++); |
| 13 | Β Β Β x = x + 2 +Β n; |
| 14 | Β Β Β sum = sum + x + y; |
| 15 | Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| 16 | Β } |
| 17 | } |
| 18 | class B extends A { |
| 19 | Β public int x = 1; |
| 20 | Β public int sum = 2; |
| 21 | Β public B(){ |
| 22 | Β Β Β y = temp + 3 ; |
| 23 | Β Β Β sum = 3 + temp + 2; |
| 24 | Β Β Β temp-=1; |
| 25 | Β }Β Β |
| 26 | Β public B(B b){ |
| 27 | Β Β Β sum = b.sum; |
| 28 | Β Β Β x = b.x; |
| 29 | Β } |
| 30 | Β public void methodB(int m, int n){Β Β Β Β |
| 31 | Β Β Β intΒ y =0; |
| 32 | Β Β Β y = y + this.y;Β |
| 33 | Β Β Β x = this.y + 2 + temp; |
| 34 | Β Β Β methodA(x, y); |
| 35 | Β Β Β sum = x + y + super.sum; |
| 36 | Β Β Β System.out.println(x + β β + y+ β β + sum); |
| 37 | Β } |
| 38 | } |
Consider the following code:
| AΒ Β Β Β Β Β Β a1 = new A();
BΒ Β Β Β Β Β Β b1 = new B(); B b2 = new B(b1); a1.methodA(1, 1); b1.methodA(1, 2); b2.methodB(3, 2); |
x | Y | sum |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β |
Task 15
| class A{ |
| Β Β Β public static int temp = 3; |
| Β Β Β public int sum; |
| Β Β Β public int y; |
| Β Β Β public A(){ |
| Β Β Β Β Β Β Β y = temp β 1; |
| Β Β Β Β Β Β Β sum = temp + 2; |
| Β Β Β Β Β Β Β temp-=2; |
| Β Β Β } |
| Β Β Β public void methodA(int m, int [] n){ |
| Β Β Β Β Β Β Β int x = 0; |
| Β Β Β Β Β Β Β y = y + m + (temp++); |
| Β Β Β Β Β Β Β x = x + 2 +Β (++n[0]); |
| Β Β Β Β Β Β Β sum = sum + x + y; |
| Β Β Β Β Β Β Β n[0] = sum + 2; |
| Β Β Β Β Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| Β Β Β } |
| } |
| class B extends A { |
| Β Β Β public static int x = 1; |
| Β Β Β public int sum = 2; |
| Β Β Β public B(){ |
| Β Β Β Β Β Β Β y = temp + 1 ; |
| Β Β Β Β Β Β Β x = 3 + temp + x; |
| Β Β Β Β Β Β Β temp-=2; |
| Β Β Β }Β Β |
| Β Β Β public B(B b){ |
| Β Β Β Β Β Β Β sum = b.sum + super.sum; |
| Β Β Β Β Β Β Β x = b.x + x; |
| Β Β Β } |
| Β Β Β public void methodB(int m, int n){Β Β Β Β |
| Β Β Β Β Β Β Β int [] y = {0}; |
| Β Β Β Β Β Β Β super.y = y[0] + this.y + m;Β |
| Β Β Β Β Β Β Β x = super.y + 2 + temp β n; |
| Β Β Β Β Β Β Β methodA(x, y); |
| Β Β Β Β Β Β Β sum = x + y[0] + super.sum; |
| Β Β Β Β Β Β Β System.out.println(x + β β + y[0]+ β β + sum); |
| Β Β Β } |
| } |
Consider the following code:
| int x[] = {23}; A a1 = new A();
B b1 = new B(); B b2 = new B(b1); |
Β | Β | Β |
| Β | Β | Β | |
| Β | Β | Β | |
| a1.methodA(1, x); b2.methodB(3, 2); a1.methodA(1, x); | Β | Β | Β |
| Β | Β | Β |
Β
Β
Β
Β
Task 16
| public class FinalT6A{ |
| Β public static int temp = 4; |
| Β private int sum; |
| Β private int y = 1; |
| Β public FinalT6A(int x, int p){ |
| Β Β Β temp+=1; |
| Β Β Β y = temp β p; |
| Β Β Β sum = temp + x; |
| Β Β Β System.out.println(x + β β + y+ β β + sum); |
| Β } |
| Β public void methodA(){Β Β Β Β |
| Β Β Β int x=0, y =0; |
| Β Β Β y = y + this.y;Β |
| Β Β Β x = this.y + 2 + temp; |
| Β Β Β sum = x + y + methodB(temp, y); |
| Β Β Β System.out.println(x + β β + y+ β β + sum); |
| Β } |
| Β public int methodB(int temp, int n){ |
| Β Β Β int x = 0; |
| Β Β Β y = y + (++temp); |
| Β Β Β x = x + 3 +Β n; |
| Β Β Β sum = sum + x + y; |
| Β Β Β System.out.println(x + β β + y+ β β + sum);Β Β |
| Β Β Β return sum; |
| Β } |
| } |
What is the output of the following code sequence?
Β
| FinalT6A q1 = new FinalT6A(2,1); q1.methodA(); q1.methodA();Β Β | x | y | sum |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β | |
| Β | Β | Β |
Β
Β
Task 17
Mutant Flatworld Explorers http://online-judge.uva.es/p/v1/118.html
Hint: It is similar to Task 1 and 2 but here you will have to maintain a two dimensional character array
Β
Background
Robotics, robot motion planning, and machine learning are areas that cross the boundaries of many of the subdisciplines that comprise Computer Science: artificial intelligence, algorithms and complexity, electrical and mechanical engineering to name a few. In addition, robots as βturtlesβ (inspired by work by Papert, Abelson, and diSessa) and as βbeeper-pickersβ (inspired by work by Pattis) have been studied and used by students as an introduction to programming for many years.
Β
This problem involves determining the position of a robot exploring a pre-Columbian flat world.
Β
The Problem
Given the dimensions of a rectangular grid and a sequence of robot positions and instructions, you are to write a program that determines for each sequence of robot positions and instructions the final position of the robot.
Β
A robot position consists of a grid coordinate (a pair of integers: x-coordinate followed by ycoordinate) and an orientation (N,S,E,W for north, south, east, and west). A robot instruction is a string of the letters βLβ, βRβ, and βFβ which represent, respectively, the instructions:
Β
- Left: the robot turns left 90 degrees and remains on the current grid point.
- Right: the robot turns right 90 degrees and remains on the current grid point.
- Forward: the robot moves forward one grid point in the direction of the current orientation and mantains the same orientation.
Β
The direction North corresponds to the direction from grid point (x,y) to grid point (x,y+1).Β Since the grid is rectangular and bounded, a robot that moves βoffβ an edge of the grid is lost forever. However, lost robots leave a robot βscentβ that prohibits future robots from dropping off the world at the same grid point. The scent is left at the last grid position the robot occupied before disappearing over the edge. An instruction to move βoffβ the world from a grid point from which a robot has been previously lost is simply ignored by the current robot.
Β
Hint: For your convenience, you may mark cells having the scent with βXβ or any character you like to mean forbidden cells.
Β
The Input
The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 0,0.
Β
The remaining input consists of a sequence of robot positions and instructions (two lines per robot). A position consists of two integers specifying the initial coordinates of the robot and an orientation (N,S,E,W), all separated by white space on one line. A robot instruction is a string of the letters βLβ, βRβ, and βFβ on one line.
Β
Each robot is processed sequentially, i.e., finishes executing the robot instructions before the next robot begins execution.
Β
Input is terminated by end-of-file.
Β
You may assume that all initial robot positions are within the bounds of the specified grid. The maximum value for any coordinate is 50. All instruction strings will be less than 100 characters in length.
The Output
For each robot position/instruction in the input, the output should indicate the final grid position and orientation of the robot. If a robot falls off the edge of the grid the word βLOSTβ should be printed after the position and orientation.
Β
Sample Input
5 3 1 1 E RFRFRFRF 3 2 N
FRRFLLFFRRFLL 0 3 W LLFFFLFLFL
Β
Sample Output
1 1 E
3 3 N LOST 2 3 S





