# SC2002 OOP Tutorial ## Tutorial 1 ### Qn 1: Class, Object, Attribute or Behaviour? | Name | Category | |:-------------- |:---------------------------------------------------------------------------- | | Student | Class | | NTU | Object | | Book | Class | | MichaelJackson | Object | | Age | Attribute | | Color | Class | | Work | Behaviour? | | Person | Class | | Result | Depends, in this context probably attribute | | Transformer | Idk what they mean here, could the `.map()` transformer which is a Behaviour | | Engine | Class | | Liquid | Class | | Force | Class | | Shoot | Behaviour | ### Qn 2: Model a school ```java public class School { private final String name; private final int established; private final ArrayList<AcademicStaff> acadStaff; private final ArrayList<Student> students; private final ArrayList<Course> courses; } ``` ### Qn 3: Transpile C to Java ```java import java.util.Arrays; import java.util.Scanner; public class Tutorial1 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter number of Integer elements to be sorted:"); int[] buf = new int[s.nextInt()]; for (int i = 0; i < buf.length; ++i) { System.out.println("Enter integer value for element no." + (i + 1) + ":"); buf[i] = s.nextInt(); } bubble(buf); System.out.println(Arrays.toString(buf)); } public static void bubble(int[] buf) { for (int i = buf.length - 2, temp; i >= 0; --i) { for (int j = 0; j <= i; ++j) { if (buf[j] > buf[j + 1]) { temp = buf[j]; buf[j] = buf[j + 1]; buf[j + 1] = temp; } } } } } ``` ## Tutorial 2 ### Qn1. Write a class Circle that has the following instance variables and methods: ```java // Circle.java public class Circle { private double radius; private static final double PI = 3.14159; public Circle(double radius) { this.radius = radius; } public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; } public double area() { return (this.radius * this.radius) * Circle.PI; } public double circumference() { return Circle.PI * 2 * this.radius; } public void printArea() { System.out.println("Area: " + this.area()); } public void printCircumference() { System.out.println("Circumference: " + this.circumference()); } } ``` ```java // CircleApp.java import java.util.Scanner; public class CircleApp { public static void main(String[] args) { System.out.println("==== Circle Computation ===="); System.out.println("|1. Create a new circle |"); System.out.println("|2. Print Area |"); System.out.println("|3. Print Circumference |"); System.out.println("|4. Quit |"); System.out.println("============================"); Scanner s = new Scanner(System.in); Circle c = null; loop: while (true) { System.out.println("Choose option (1-3)"); switch (s.nextInt()) { case 1: System.out.println("Enter the radius to compute the area and circumference"); c = new Circle(s.nextInt()); break; case 2: if(c == null) { System.out.println("No circle created! Choose option 1"); continue; } c.printArea(); break; case 3: if(c == null) { System.out.println("No circle created! Choose option 1"); continue; } c.printCircumference(); break; default: System.out.println("Thank you!!"); break loop; } } } } ``` ## Qn2: Write a class Dice that has the following instance variables and methods: ```java // Dice.java public class Dice { private int valueOfDice; public Dice(int valueOfDice) { this.valueOfDice = valueOfDice; } public int getValueOfDice() { return valueOfDice; } public void setValueOfDice(int valueOfDice) { this.valueOfDice = valueOfDice; } public void printDiceValue() { System.out.println("Dice value: " + this.valueOfDice); } } ``` ```java // DiceApp.java import java.util.Random; import java.util.Scanner; public class DiceApp { public static void main(String[] args) { Random r = new Random(); Scanner s = new Scanner(System.in); Dice d1 = new Dice(r.nextInt(Integer.MAX_VALUE)), d2 = new Dice(r.nextInt(Integer.MAX_VALUE)); System.out.println("Press <key> to roll first dice"); s.nextInt(); System.out.println("Current Value is: " + d1.getValueOfDice()); System.out.println("Press <key> to roll second dice"); s.nextInt(); System.out.println("Current Value is: " + d2.getValueOfDice()); long total = d1.getValueOfDice() + d2.getValueOfDice(); System.out.println("Your total number is: " + total); } } ```