From 67393f42f41ab92219deb549f711121c4dab845b Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 15 Dec 2019 10:34:58 +0700 Subject: [usth/ICT2.2] Object Oriented Programming --- usth/ICT2.2/final/Member.java | 54 +++++++++++++++++++++++++++++++++++++++++ usth/ICT2.2/final/Point.java | 43 ++++++++++++++++++++++++++++++++ usth/ICT2.2/final/Problem1.java | 15 ++++++++++++ usth/ICT2.2/final/Problem2.java | 22 +++++++++++++++++ usth/ICT2.2/final/Problem3.java | 40 ++++++++++++++++++++++++++++++ usth/ICT2.2/final/Report.java | 18 ++++++++++++++ 6 files changed, 192 insertions(+) create mode 100644 usth/ICT2.2/final/Member.java create mode 100644 usth/ICT2.2/final/Point.java create mode 100644 usth/ICT2.2/final/Problem1.java create mode 100644 usth/ICT2.2/final/Problem2.java create mode 100644 usth/ICT2.2/final/Problem3.java create mode 100644 usth/ICT2.2/final/Report.java (limited to 'usth/ICT2.2/final') diff --git a/usth/ICT2.2/final/Member.java b/usth/ICT2.2/final/Member.java new file mode 100644 index 0000000..a3e2092 --- /dev/null +++ b/usth/ICT2.2/final/Member.java @@ -0,0 +1,54 @@ +public class Member +{ + public static final String[] ROLES = {"leader", "club coordinator", + "keynote speaker of code sharing", + "product programmer"}; + + private String name; + private String time; + private int role; + + public String getName() + { + return name; + } + + public String getTime() + { + return time; + } + + public int getRole() + { + return role; + } + + public void setName(String name) + { + this.name = name; + } + + public void setTime(String time) + { + this.time = time; + } + + public void setRole(int role) throws IllegalArgumentException + { + if (role < 0 || role > 3) + throw new IllegalArgumentException("invalid role number: " + role); + this.role = role; + } + + public Member(String name, String time, int role) + { + setName(name); + setTime(time); + setRole(role); + } + + public String toString() + { + return String.format("%s (%s), from %s", name, ROLES[role], time); + } +} diff --git a/usth/ICT2.2/final/Point.java b/usth/ICT2.2/final/Point.java new file mode 100644 index 0000000..5e79428 --- /dev/null +++ b/usth/ICT2.2/final/Point.java @@ -0,0 +1,43 @@ +// Immutable Point +public class Point +{ + private double x; + private double y; + + public Point(double x, double y) + { + this.x = x; + this.y = y; + } + + public double getX() + { + return x; + } + + public double getY() + { + return y; + } + + public String toString() + { + return String.format("(%g, %g)", x, y); + } + + public static Point add(Point a, Point b) + { + return new Point(a.getX() + b.getX(), a.getY() + b.getY()); + } + + public static Point subtract(Point a, Point b) + { + return new Point(a.getX() - b.getX(), a.getY() - b.getY()); + } + + public static double calDisEuclid(Point a, Point b) + { + var trans = Point.subtract(a, b); + return Math.sqrt(trans.getX()*trans.getX() + trans.getY()*trans.getY()); + } +} diff --git a/usth/ICT2.2/final/Problem1.java b/usth/ICT2.2/final/Problem1.java new file mode 100644 index 0000000..a9c5e32 --- /dev/null +++ b/usth/ICT2.2/final/Problem1.java @@ -0,0 +1,15 @@ +import java.util.Scanner; + +class Problem1 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + int n = scanner.nextInt(); + for (int i = 1; i <= n; i += 2) + System.out.println(i); + if (n % 2 == 0) + n--; + System.out.println((n + 1) * (n + 1) / 4); + } +} diff --git a/usth/ICT2.2/final/Problem2.java b/usth/ICT2.2/final/Problem2.java new file mode 100644 index 0000000..dcad796 --- /dev/null +++ b/usth/ICT2.2/final/Problem2.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +class Problem2 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + System.out.println( + "Please enter two numbers which are the coordinate of one point:"); + var a = new Point(scanner.nextDouble(), scanner.nextDouble()); + System.out.println( + "Please enter two numbers which are the coordinate of another point:"); + var b = new Point(scanner.nextDouble(), scanner.nextDouble()); + + System.out.printf("First point: %s\nSecond point: %s\n", a, b); + // Vector would make a better name than Point + System.out.printf("Their sum: %s\nTheir difference: %s\n", + Point.add(a, b), Point.subtract(a, b)); + System.out.printf("The Euclidean distance between the two points: %s\n", + Point.calDisEuclid(a, b)); + } +} diff --git a/usth/ICT2.2/final/Problem3.java b/usth/ICT2.2/final/Problem3.java new file mode 100644 index 0000000..11be143 --- /dev/null +++ b/usth/ICT2.2/final/Problem3.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Problem3 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + System.out.println("Number of members:"); + int n = scanner.nextInt(); + + var members = new ArrayList(); + for (int i = 0; i < n; ++i) + { + System.out.println("Member name: "); + String name = scanner.next(); + System.out.println("Member joining time: "); + String time = scanner.next(); + System.out.println("Member role number: "); + int role = scanner.nextInt(); + members.add(new Member(name, time, role)); + } + + var reports = new ArrayList(); + for (var m : members) + { + System.out.println(m); + System.out.println("Activity: "); + String act = scanner.next(); + System.out.println("Rate: "); + int rate = scanner.nextInt(); + reports.add(new Report(m.getName(), act, rate)); + } + + System.out.println("Members:"); + members.forEach(System.out::println); + System.out.println("Activity reports:"); + reports.forEach(System.out::println); + } +} diff --git a/usth/ICT2.2/final/Report.java b/usth/ICT2.2/final/Report.java new file mode 100644 index 0000000..ec4ee23 --- /dev/null +++ b/usth/ICT2.2/final/Report.java @@ -0,0 +1,18 @@ +public class Report +{ + String name; + String activity; + int rate; + + public Report(String name, String act, int rate) + { + this.name = name; + this.activity = act; + this.rate = rate; + } + + public String toString() + { + return String.format("%s: %s: %s", name, activity, rate); + } +} -- cgit 1.4.1