[usth/ICT2.2] Object Oriented Programming
This commit is contained in:
parent
b38d9929f7
commit
67393f42f4
32
README.md
32
README.md
|
@ -2,21 +2,22 @@
|
|||
|
||||
Bài tập luyện tập thi Olympic, học sinh giỏi Tin học, trong đó:
|
||||
|
||||
| Thư mục | Nguồn đề bài |
|
||||
| ---------------------- | ------------------------------------------------- |
|
||||
| `09`, `10`, `11`, `12` | Đề thi, kiểm tra phân theo lớp |
|
||||
| `COCI` | [Giải Tin học Croatia mở rộng][0] |
|
||||
| `NTU` | [Đại học Nha Trang][1] |
|
||||
| `THT` | Hội thi Tin học trẻ |
|
||||
| `codechef` | [Codechef][2] |
|
||||
| `codeforces` | [Codeforces][3] |
|
||||
| `cpptour` | A Tour of C++ |
|
||||
| `daily` | [/r/dailyprogrammer][4] |
|
||||
| `others` | Các đề bài không rõ nguồn |
|
||||
| `paip` | Paradigms of Artificial Intelligence Programming |
|
||||
| `sicp` | Structure and Interpretation of Computer Programs |
|
||||
| `thinkperl6` | Think Perl 6 |
|
||||
| `toys` | Programs that don't deserve their own repo |
|
||||
| Thư mục | Nguồn đề bài |
|
||||
| ------------ | ------------------------------------------------------ |
|
||||
| `\d{2}` | Đề thi, kiểm tra phân theo lớp |
|
||||
| `COCI` | [Giải Tin học Croatia mở rộng][0] |
|
||||
| `NTU` | [Đại học Nha Trang][1] |
|
||||
| `THT` | Hội thi Tin học trẻ |
|
||||
| `codechef` | [Codechef][2] |
|
||||
| `codeforces` | [Codeforces][3] |
|
||||
| `cpptour` | A Tour of C++ |
|
||||
| `daily` | [/r/dailyprogrammer][4] |
|
||||
| `others` | Các đề bài không rõ nguồn |
|
||||
| `paip` | Paradigms of Artificial Intelligence Programming |
|
||||
| `sicp` | Structure and Interpretation of Computer Programs |
|
||||
| `thinkperl6` | Think Perl 6 |
|
||||
| `toys` | Programs that don't deserve their own repo |
|
||||
| `usth` | L'Université des Sciences et des Technologies de Hanoï |
|
||||
|
||||
[0]: http://www.hsin.hr/coci/
|
||||
[1]: http://laptrinh.ntu.edu.vn/
|
||||
|
@ -34,6 +35,7 @@ Phiên bản các trình dịch sử dụng test:
|
|||
| ----------- | ------------------ |
|
||||
| C | GNU GCC 4.9+ |
|
||||
| Common Lisp | SBCL 1.4.8+ |
|
||||
| Java | OpenJDK 11+ |
|
||||
| Lua | Lua 5.1+ |
|
||||
| Pascal | Free Pascal 2.6.4+ |
|
||||
| Perl 6 | Rakudo 2018.12+ |
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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<Member>();
|
||||
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<Report>();
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import java.util.Arrays;
|
||||
|
||||
class AllEqual
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
String first = args[0];
|
||||
System.out.println(Arrays.stream(args).allMatch(a -> (a.equals(first))));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
class Binary
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
int n = Integer.parseInt(args[0]);
|
||||
String s = "";
|
||||
while (n > 0)
|
||||
{
|
||||
s = (n % 2) + s;
|
||||
n >>= 1;
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
class Distance
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
double x = Double.parseDouble(args[0]);
|
||||
double y = Double.parseDouble(args[1]);
|
||||
System.out.printf("The distance from (%g, %g) to origin is %g\n",
|
||||
x, y, Math.sqrt(x*x + y*y));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
class FivePerLine
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
for (int i = 1000; i < 2000; ++i)
|
||||
if (i % 5 < 4)
|
||||
System.out.print(i + " ");
|
||||
else
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
class FunctionGrowth
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.println("ln n\tn\tn ln n\tn^2\tn^3\t2^n");
|
||||
for (long n = 16; n < 3005; n <<= 1)
|
||||
System.out.printf("%g\t%d\t%g\t%d\t%d\t%g\n", Math.log(n), n,
|
||||
n * Math.log(n), n*n, n*n*n, Math.pow(2, n));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
class HelloWorld
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
class Hellos
|
||||
{
|
||||
private static final String[] TH = {"th", "st", "nd", "rd", "th",
|
||||
"th", "th", "th", "th", "th"};
|
||||
|
||||
public static void main(String... args)
|
||||
{
|
||||
int n = Integer.parseInt(args[0]);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
// Before one says this defeat the purpose of the section,
|
||||
// the tertiary operator IS conditional.
|
||||
System.out.println(i + TH[i % 100 / 10 == 1 ? 7 : i % 10] + " Hello");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
class Kary
|
||||
{
|
||||
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
|
||||
public static void main(String... args)
|
||||
{
|
||||
int n = Integer.parseInt(args[0]);
|
||||
List<Character> result = new ArrayList<Character>();
|
||||
for (int k = Integer.parseInt(args[1]); n > 0; n /= k)
|
||||
result.add(DIGITS[n % k]);
|
||||
|
||||
Collections.reverse(result);
|
||||
for (char d : result)
|
||||
System.out.print(d);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
class RollLoadedDie
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
int x = ThreadLocalRandom.current().nextInt(1, 9);
|
||||
System.out.println((x < 6) ? x : 6);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
import java.time.LocalDate;
|
||||
import java.time.DateTimeException;
|
||||
|
||||
class SpringSeason
|
||||
{
|
||||
private static final LocalDate start = LocalDate.of(42, 3, 19);
|
||||
private static final LocalDate end = LocalDate.of(42, 6, 21);
|
||||
|
||||
public static void main(String... args)
|
||||
{
|
||||
int m = Integer.parseInt(args[0]);
|
||||
int d = Integer.parseInt(args[1]);
|
||||
try
|
||||
{
|
||||
LocalDate query = LocalDate.of(42, m, d);
|
||||
// Yes, I'm sorry for leaving it here.
|
||||
// If only Java has the try-except-else like Python!
|
||||
System.out.println(query.isAfter(start) && query.isBefore(end));
|
||||
}
|
||||
catch (DateTimeException e)
|
||||
{
|
||||
System.out.println(false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
class SumOfSines
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
double t = Double.parseDouble(args[0]);
|
||||
System.out.printf("sin2t + sin3t = %g", Math.sin(t * 2) + Math.sin (t * 3));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
class SumOfTwoDice
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
int x = ThreadLocalRandom.current().nextInt(1, 7);
|
||||
System.out.println("First roll: " + x);
|
||||
int y = ThreadLocalRandom.current().nextInt(1, 7);
|
||||
System.out.println("Second roll: " + y);
|
||||
System.out.println("Sum: " + (x + y));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
class TenHelloWorld
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i)
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
class UseArguments
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.printf("Hi %s. How are you?\n", args[0]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
class UseThree
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.printf("Hi %s, %s and %s. How are you?\n",
|
||||
args[2], args[1], args[0]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
# Logical Exclusive Or Table
|
||||
|
||||
| a | b | a ^ b |
|
||||
| ----- | ----- | ----- |
|
||||
| false | false | false |
|
||||
| false | true | true |
|
||||
| true | false | true |
|
||||
| true | true | false |
|
|
@ -0,0 +1,15 @@
|
|||
# String Concatenation
|
||||
|
||||
To quote the [official Java documentation](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html):
|
||||
|
||||
> The Java language provides special support for the string concatenation
|
||||
> operator (`+`), and for conversion of other objects to strings. [...]
|
||||
> String conversions are implemented through the method `toString`, defined
|
||||
> by Object and inherited by all classes in Java.
|
||||
|
||||
Thus the numbers (e.g. 2, 2 + 3 = 5) are converted to their strings
|
||||
representations ("2", "5") and concatenated to the string. Since `+` is
|
||||
operated from left to right,
|
||||
|
||||
2 + 3 + "bc" = 5 + "bc" = "5" + "bc" = "5bc"
|
||||
"bc" + 2 + 3 = "bc" + "2" + 3 = "bc2" + 3 = "bc2" + "3" = "bc23"
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,18 @@
|
|||
# Build with Maven
|
||||
|
||||
Following the official guide [Maven in 5 Minutes](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html):
|
||||
|
||||
1. Create a project named `my-app`:
|
||||
|
||||
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
|
||||
|
||||
2. Change into the project directory: `cd my-app`
|
||||
3. Build the project: `mvn package`
|
||||
|
||||
Extra exercises for labwork 1 are written in `src/main/java/com/mycompany/app/`
|
||||
inside `my-app`. To launch a class named `App` for example, run
|
||||
|
||||
java -cp my-app/target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
|
||||
|
||||
I find this build method to be overcomplicated for 5-minute throw-away programs
|
||||
and decide not to use it in the upcoming labworks.
|
|
@ -0,0 +1,75 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.mycompany.app</groupId>
|
||||
<artifactId>my-app</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>my-app</name>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<url>http://www.example.com</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,9 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
// Exercise 4
|
||||
public class Beers
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
for (int i = 9; i > 1; --i)
|
||||
System.out.printf(
|
||||
"%d bottles of beer we are going to drink, %d bottles of beer.\n"
|
||||
+ "Now try to drink one, drink one,\n", i, i);
|
||||
System.out.print(
|
||||
"1 bottle of beer we are going to drink, 1 bottle of beer.\n"
|
||||
+ "Now try to drink one, drink one,\n"
|
||||
+ "Oh no, no bottles of beer to drink now.\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
// Exercise 7
|
||||
public class IsLeapYear
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
int n = Integer.parseInt(args[0]);
|
||||
if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0)
|
||||
System.out.printf("%d is a leap year\n", n);
|
||||
else
|
||||
System.out.printf("%d is not a leap year\n", n);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
// Exercise 5
|
||||
public class Linear
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
double a = Double.parseDouble(args[0]);
|
||||
double b = Double.parseDouble(args[1]);
|
||||
System.out.println(-b / a);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
// Exercise 8
|
||||
public class MeanSTD
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
double n = args.length;
|
||||
double m = Arrays.stream(args).mapToDouble(Double::parseDouble).sum() / n;
|
||||
double s = Math.sqrt(
|
||||
Arrays.stream(args)
|
||||
.mapToDouble(x -> Math.pow(Double.parseDouble(x) - m, 2)).sum() / n);
|
||||
System.out.printf("Mean: %f\nStandard deviation: %f\n", m, s);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
// Exercise 6
|
||||
public class Quadratic
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
double a = Double.parseDouble(args[0]);
|
||||
double b = Double.parseDouble(args[1]);
|
||||
double c = Double.parseDouble(args[2]);
|
||||
// assume ax^2 + bx + c = 0 is a valid quadratic equation
|
||||
double d = b*b - a*c*4;
|
||||
if (d < 0)
|
||||
{
|
||||
System.out.printf("%f + %fj\n", -b/a/2, Math.sqrt(-d)/a/2);
|
||||
System.out.printf("%f + %fj\n", -b/a/2, -Math.sqrt(-d)/a/2);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(-b/a/2 + Math.sqrt(d)/a/2);
|
||||
System.out.println(-b/a/2 - Math.sqrt(d)/a/2);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
// Exercise 3
|
||||
class RandRange
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.println(
|
||||
ThreadLocalRandom.current().nextInt(0, Integer.parseInt(args[0])));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
public class TenHellos
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i)
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
// Exercise 2
|
||||
class UseThree
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.printf("Hi %s, %s and %s. How are you?\n",
|
||||
args[2], args[1], args[0]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.mycompany.app;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
// Unit test for simple App.
|
||||
public class AppTest
|
||||
{
|
||||
// Rigorous Test :-)
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,4 @@
|
|||
#Created by Apache Maven 3.6.2
|
||||
groupId=com.mycompany.app
|
||||
artifactId=my-app
|
||||
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,9 @@
|
|||
com/mycompany/app/App.class
|
||||
com/mycompany/app/UseThree.class
|
||||
com/mycompany/app/Linear.class
|
||||
com/mycompany/app/TenHellos.class
|
||||
com/mycompany/app/MeanSTD.class
|
||||
com/mycompany/app/RandRange.class
|
||||
com/mycompany/app/Beers.class
|
||||
com/mycompany/app/IsLeapYear.class
|
||||
com/mycompany/app/Quadratic.class
|
|
@ -0,0 +1,9 @@
|
|||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java
|
||||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/UseThree.java
|
|
@ -0,0 +1 @@
|
|||
com/mycompany/app/AppTest.class
|
|
@ -0,0 +1 @@
|
|||
/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/test/java/com/mycompany/app/AppTest.java
|
Binary file not shown.
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.mycompany.app.AppTest" time="0.019" tests="1" errors="0" skipped="0" failures="0">
|
||||
<properties>
|
||||
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
|
||||
<property name="java.specification.version" value="11"/>
|
||||
<property name="sun.cpu.isalist" value=""/>
|
||||
<property name="sun.jnu.encoding" value="UTF-8"/>
|
||||
<property name="java.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/test-classes:/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/classes:/home/cnx/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/cnx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
|
||||
<property name="java.vm.vendor" value="Debian"/>
|
||||
<property name="sun.arch.data.model" value="64"/>
|
||||
<property name="java.vendor.url" value="https://tracker.debian.org/openjdk-11"/>
|
||||
<property name="user.timezone" value=""/>
|
||||
<property name="java.vm.specification.version" value="11"/>
|
||||
<property name="os.name" value="Linux"/>
|
||||
<property name="sun.java.launcher" value="SUN_STANDARD"/>
|
||||
<property name="user.country" value="US"/>
|
||||
<property name="sun.boot.library.path" value="/usr/lib/jvm/java-11-openjdk-amd64/lib"/>
|
||||
<property name="sun.java.command" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire/surefirebooter4171187558944627507.jar /home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire 2019-10-09T17-47-23_851-jvmRun1 surefire1053437032396327338tmp surefire_014762799941833427558tmp"/>
|
||||
<property name="jdk.debug" value="release"/>
|
||||
<property name="surefire.test.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/test-classes:/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/classes:/home/cnx/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/cnx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
|
||||
<property name="sun.cpu.endian" value="little"/>
|
||||
<property name="user.home" value="/home/cnx"/>
|
||||
<property name="user.language" value="en"/>
|
||||
<property name="java.specification.vendor" value="Oracle Corporation"/>
|
||||
<property name="java.version.date" value="2019-10-15"/>
|
||||
<property name="java.home" value="/usr/lib/jvm/java-11-openjdk-amd64"/>
|
||||
<property name="file.separator" value="/"/>
|
||||
<property name="basedir" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app"/>
|
||||
<property name="java.vm.compressedOopsMode" value="32-bit"/>
|
||||
<property name="line.separator" value=" "/>
|
||||
<property name="java.specification.name" value="Java Platform API Specification"/>
|
||||
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
|
||||
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
|
||||
<property name="surefire.real.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire/surefirebooter4171187558944627507.jar"/>
|
||||
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
|
||||
<property name="java.runtime.version" value="11.0.5-ea+6-post-Debian-2"/>
|
||||
<property name="user.name" value="cnx"/>
|
||||
<property name="path.separator" value=":"/>
|
||||
<property name="os.version" value="4.19.0-5-amd64"/>
|
||||
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
|
||||
<property name="file.encoding" value="UTF-8"/>
|
||||
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
|
||||
<property name="localRepository" value="/home/cnx/.m2/repository"/>
|
||||
<property name="java.vendor.url.bug" value="https://bugs.debian.org/openjdk-11"/>
|
||||
<property name="java.io.tmpdir" value="/tmp"/>
|
||||
<property name="java.version" value="11.0.5-ea"/>
|
||||
<property name="user.dir" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app"/>
|
||||
<property name="os.arch" value="amd64"/>
|
||||
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
|
||||
<property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
|
||||
<property name="sun.os.patch.level" value="unknown"/>
|
||||
<property name="java.library.path" value="/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/>
|
||||
<property name="java.vendor" value="Debian"/>
|
||||
<property name="java.vm.info" value="mixed mode, sharing"/>
|
||||
<property name="java.vm.version" value="11.0.5-ea+6-post-Debian-2"/>
|
||||
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
|
||||
<property name="java.class.version" value="55.0"/>
|
||||
</properties>
|
||||
<testcase name="shouldAnswerWithTrue" classname="com.mycompany.app.AppTest" time="0.001"/>
|
||||
</testsuite>
|
|
@ -0,0 +1,4 @@
|
|||
-------------------------------------------------------------------------------
|
||||
Test set: com.mycompany.app.AppTest
|
||||
-------------------------------------------------------------------------------
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in com.mycompany.app.AppTest
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,10 @@
|
|||
# Labwork 3: Implementations in C++
|
||||
|
||||
At this point I've come to realized that like Thanos, C++ is inevitable.
|
||||
Despite my disgust toward the language (TBH I don't like Java any better),
|
||||
I need to know it enough, at least at the read-only level.
|
||||
|
||||
Compilation of tests could be done as follows (on Unix-like system of course,
|
||||
since poor Windows users don't even have a standard C/C++ compiler):
|
||||
|
||||
c++ <classname>*.cc
|
|
@ -0,0 +1,56 @@
|
|||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "automobile.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const regex license_pattern ("[0-9A-Z]+");
|
||||
|
||||
void
|
||||
Automobile::set_license (string s)
|
||||
{
|
||||
smatch m;
|
||||
if (!regex_match (s, m, license_pattern))
|
||||
throw invalid_argument{"invalid license plate"};
|
||||
license = s;
|
||||
}
|
||||
|
||||
void
|
||||
Automobile::set_fuel (double x)
|
||||
{
|
||||
if (x < 0)
|
||||
throw invalid_argument{"negative fuel"};
|
||||
fuel = x;
|
||||
}
|
||||
|
||||
void
|
||||
Automobile::set_speed (double x)
|
||||
{
|
||||
speed = (x < 0) ? 0 : x;
|
||||
}
|
||||
|
||||
Automobile::Automobile (string l, double f, double s)
|
||||
{
|
||||
set_license (l);
|
||||
set_fuel (f);
|
||||
set_speed (s);
|
||||
}
|
||||
|
||||
void
|
||||
Automobile::accelerate (double v)
|
||||
{
|
||||
if (v < 0)
|
||||
throw invalid_argument{"negative acceleration"};
|
||||
if (fuel)
|
||||
set_speed (speed + v);
|
||||
}
|
||||
|
||||
void
|
||||
Automobile::decelerate (double v)
|
||||
{
|
||||
if (v < 0)
|
||||
throw invalid_argument{"negative deceleration"};
|
||||
set_speed (speed - v);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Automobile
|
||||
{
|
||||
double fuel;
|
||||
double speed;
|
||||
string license;
|
||||
public:
|
||||
Automobile (string, double, double);
|
||||
string get_license () { return license; }
|
||||
double get_fuel () { return fuel; }
|
||||
double get_speed () { return speed; }
|
||||
void set_license (string);
|
||||
void set_fuel (double);
|
||||
void set_speed (double);
|
||||
void accelerate (double);
|
||||
void decelerate (double);
|
||||
};
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "button.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
Button butt ("foo", "bar");
|
||||
butt.depress ();
|
||||
butt.undepress ();
|
||||
cout << "button " << butt.label << " color " << butt.color << endl;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Button
|
||||
{
|
||||
bool state;
|
||||
public:
|
||||
string label;
|
||||
string color;
|
||||
Button (string l, string c) : label {l}, color {c}, state {false} {}
|
||||
void depress () { state = true; }
|
||||
void undepress () { state = false; }
|
||||
};
|
|
@ -0,0 +1,9 @@
|
|||
#include "cow.h"
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
Cow cow ("foo", "bar", 7);
|
||||
cow.age = -4; // some casting happens here
|
||||
cow.moo ();
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "cow.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
Cow::moo ()
|
||||
{
|
||||
cout << "Moo...!" << endl;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Cow
|
||||
{
|
||||
// The reason these have private access
|
||||
// is that their no way a cow's name and breed can be changed.
|
||||
string name;
|
||||
string breed;
|
||||
public:
|
||||
unsigned age;
|
||||
|
||||
Cow (string n, string b, unsigned a) : name {n}, breed {b}, age {a} {}
|
||||
Cow (string n, string b) : name {n}, breed {b}, age {0} {}
|
||||
void moo ();
|
||||
};
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "name-card.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
NameCard card ("Foobar Baz", "420-69", "foo@bar.baz");
|
||||
cout << "Name: " << card.get_name () << endl
|
||||
<< "Phone: " << card.get_phone () << endl
|
||||
<< "Email: " << card.get_email () << endl;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "name-card.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const regex name_pattern ("[ A-Za-z]+");
|
||||
const regex phone_pattern ("[-0-9]+");
|
||||
// This should be good enough I guess?
|
||||
const regex email_pattern ("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
|
||||
|
||||
void
|
||||
NameCard::set_name (string s)
|
||||
{
|
||||
// I miss Raku so much
|
||||
smatch m;
|
||||
if (!regex_match (s, m, name_pattern))
|
||||
throw invalid_argument{"invalid name"};
|
||||
name = s;
|
||||
}
|
||||
|
||||
void
|
||||
NameCard::set_phone (string s)
|
||||
{
|
||||
smatch m;
|
||||
if (!regex_match (s, m, phone_pattern))
|
||||
throw invalid_argument{"invalid number"};
|
||||
phone = s;
|
||||
}
|
||||
|
||||
void
|
||||
NameCard::set_email (string s)
|
||||
{
|
||||
smatch m;
|
||||
if (!regex_match (s, m, email_pattern))
|
||||
throw invalid_argument{"invalid name"};
|
||||
email = s;
|
||||
}
|
||||
|
||||
NameCard::NameCard (string n, string p, string e)
|
||||
{
|
||||
set_name (n);
|
||||
set_phone (p);
|
||||
set_email (e);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class NameCard
|
||||
{
|
||||
string name;
|
||||
string phone;
|
||||
string email;
|
||||
public:
|
||||
string get_name () { return name; }
|
||||
string get_phone () { return phone; }
|
||||
string get_email () { return email; }
|
||||
void set_name (string);
|
||||
void set_phone (string);
|
||||
void set_email (string);
|
||||
NameCard (string, string, string);
|
||||
};
|
|
@ -0,0 +1,19 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "shopping-cart.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
ShoppingCart cart;
|
||||
cart.add_to_cart ("foo");
|
||||
cart.add_to_cart ("foo");
|
||||
cart.add_to_cart ("bar");
|
||||
cart.remove_from_cart ("baz");
|
||||
cout << cart.count ("foo") << ' '
|
||||
<< cart.count ("bar") << ' '
|
||||
<< cart.count ("baz") << endl;
|
||||
cart.check_out ();
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ShoppingCart
|
||||
{
|
||||
// quite of an improvement over the Java version
|
||||
unordered_map<string, int> contents;
|
||||
public:
|
||||
void add_to_cart (string item) { contents[item]++; }
|
||||
void remove_from_cart (string item) { contents[item] = 0; }
|
||||
// to make this class useful anyhow
|
||||
int count (string item) { return contents[item]; }
|
||||
void check_out () { contents.clear (); }
|
||||
};
|
|
@ -0,0 +1,20 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
Vector u (4, 20);
|
||||
Vector v (6, 9);
|
||||
cout << "u = (" << u.x << ", " << u.y << ")" << endl;
|
||||
cout << "v = (" << v.x << ", " << v.y << ")" << endl;
|
||||
Vector w {u + v};
|
||||
cout << "u + v = (" << w.x << ", " << w.y << ")" << endl;
|
||||
w = u - v;
|
||||
cout << "u - v = (" << w.x << ", " << w.y << ")" << endl;
|
||||
w = u * v;
|
||||
cout << "u * v = (" << w.x << ", " << w.y << ")" << endl;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#include "vector.h"
|
||||
|
||||
Vector
|
||||
operator+ (Vector u, Vector v)
|
||||
{
|
||||
return u += v;
|
||||
}
|
||||
|
||||
Vector
|
||||
operator- (Vector u, Vector v)
|
||||
{
|
||||
return u -= v;
|
||||
}
|
||||
|
||||
Vector
|
||||
operator* (Vector u, Vector v)
|
||||
{
|
||||
return u *= v;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
class Vector
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
|
||||
Vector (int ex, int why) : x {ex}, y {why} {}
|
||||
Vector () : x {0}, y{0} {}
|
||||
|
||||
Vector& operator+= (Vector v) { x += v.x, y += v.y; return *this; }
|
||||
Vector& operator-= (Vector v) { x -= v.x, y -= v.y; return *this; }
|
||||
Vector& operator*= (Vector v) { x *= v.x, y *= v.y; return *this; }
|
||||
};
|
||||
|
||||
Vector operator+ (Vector, Vector);
|
||||
Vector operator- (Vector, Vector);
|
||||
Vector operator* (Vector, Vector);
|
|
@ -0,0 +1,68 @@
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
class Automobile
|
||||
{
|
||||
private static final Pattern licensePattern = Pattern.compile("[0-9A-Z]+");
|
||||
private double fuel;
|
||||
private double speed;
|
||||
private String license;
|
||||
|
||||
public double getFuel()
|
||||
{
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public double getSpeed()
|
||||
{
|
||||
return speed;
|
||||
}
|
||||
|
||||
public String getLicense()
|
||||
{
|
||||
return license;
|
||||
}
|
||||
|
||||
public void setFuel(double fuel)
|
||||
{
|
||||
if (fuel < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"fuel must be nonnegative, instead got " + fuel);
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
public void setSpeed(double speed)
|
||||
{
|
||||
this.speed = Math.max(0, speed);
|
||||
}
|
||||
|
||||
public void setLicense(String license)
|
||||
{
|
||||
if (!licensePattern.matcher(license).matches())
|
||||
throw new IllegalArgumentException("invalid license: " + license);
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
public Automobile(double f, double s, String l)
|
||||
{
|
||||
setFuel(f);
|
||||
setSpeed(s);
|
||||
setLicense(l);
|
||||
}
|
||||
|
||||
public void accelerate(double v)
|
||||
{
|
||||
if (v < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"acceleration must be nonnegative, instead got " + v);
|
||||
if (fuel > 0)
|
||||
setSpeed(speed + v);
|
||||
}
|
||||
|
||||
public void decelerate(double v)
|
||||
{
|
||||
if (v < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"deceleration must be nonnegative, instead got " + v);
|
||||
setSpeed(speed - v);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
public class Button
|
||||
{
|
||||
private String label;
|
||||
private String color;
|
||||
private boolean state;
|
||||
|
||||
public Button(String label, String color)
|
||||
{
|
||||
this.label = label;
|
||||
this.color = color;
|
||||
this.state = false;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return String.format("<button %s with color %s and state %s>",
|
||||
label, color, state);
|
||||
}
|
||||
|
||||
// To be honest these getters and setters are really redundant in this case
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public String getColor()
|
||||
{
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setLabel(String label)
|
||||
{
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public void setColor(String color)
|
||||
{
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void dePress()
|
||||
{
|
||||
this.state = true;
|
||||
}
|
||||
|
||||
public void unDepress()
|
||||
{
|
||||
this.state = false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
class ButtonTest
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
Button button = new Button("foo", "bar");
|
||||
System.out.println(button);
|
||||
button.setLabel("fu");
|
||||
button.setColor("baz");
|
||||
button.dePress();
|
||||
System.out.println(button);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
public class Cow
|
||||
{
|
||||
private String name;
|
||||
private String breed;
|
||||
private int age;
|
||||
|
||||
public Cow(String name, String breed)
|
||||
{
|
||||
this(name, breed, 0);
|
||||
}
|
||||
|
||||
public Cow(String name, String breed, int age)
|
||||
{
|
||||
this.name = name;
|
||||
this.breed = breed;
|
||||
setAge(age);
|
||||
}
|
||||
|
||||
public static void moo()
|
||||
{
|
||||
System.out.println("Moo...!");
|
||||
}
|
||||
|
||||
public int getAge()
|
||||
{
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age)
|
||||
{
|
||||
if (age < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"age must be nonnegative, instead got " + age);
|
||||
this.age = age;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
class CowTest // there's no reason to import a test what-so-ever
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
Cow cow = new Cow("foo", "bar", 7);
|
||||
cow.setAge(-4);
|
||||
cow.moo();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
public class NameCard
|
||||
{
|
||||
private static final Pattern namePattern = Pattern.compile("[ A-Za-z]+");
|
||||
private static final Pattern phonePattern = Pattern.compile("[-0-9]*");
|
||||
// I have not learnt regex properly
|
||||
private static final Pattern emailPattern = Pattern.compile(
|
||||
"(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
|
||||
|
||||
private String name;
|
||||
private String phone;
|
||||
private String email;
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getPhone()
|
||||
{
|
||||
return phone;
|
||||
}
|
||||
|
||||
public String getEmail()
|
||||
{
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
if (!namePattern.matcher(name).matches())
|
||||
throw new IllegalArgumentException("invalid name: " + name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setPhone(String phone)
|
||||
{
|
||||
if (!phonePattern.matcher(phone).matches())
|
||||
throw new IllegalArgumentException("invalid phone number: " + phone);
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public void setEmail(String email)
|
||||
{
|
||||
if (!emailPattern.matcher(email).matches())
|
||||
throw new IllegalArgumentException("invalid email: " + email);
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public NameCard(String name)
|
||||
{
|
||||
this(name, "", "");
|
||||
}
|
||||
|
||||
public NameCard(String name, String phone)
|
||||
{
|
||||
this(name, phone, "");
|
||||
}
|
||||
|
||||
public NameCard(String name, String phone, String email)
|
||||
{
|
||||
setName(name);
|
||||
setPhone(phone);
|
||||
setEmail(email);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
class NameCardTest
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
NameCard card = new NameCard("Foobar Baz", "420-69", "foo@bar.baz");
|
||||
System.out.printf("Name: %s\nPhone: %s\nEmail: %s\n",
|
||||
card.getName(), card.getPhone(), card.getEmail());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
# Labwork 3: Implementations in Java
|
||||
|
||||
For the ease of typing, the test files are named `<classname>Test.java`
|
||||
instead of `<classname>TestDrive.java`.
|
||||
To rename them to the original requirement, use Perl `rename` and GNU `sed`
|
||||
|
||||
```sh
|
||||
rename s/Test/TestDrive/ *Test.java
|
||||
sed -i s/Test/TestDrive/ *TestDrive.java
|
||||
```
|
|
@ -0,0 +1,27 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShoppingCart
|
||||
{
|
||||
private List<String> cartContents = new ArrayList<String>();
|
||||
|
||||
public boolean addToCart(String content)
|
||||
{
|
||||
return cartContents.add(content);
|
||||
}
|
||||
|
||||
public boolean removeFromCart(String content)
|
||||
{
|
||||
return cartContents.remove(content);
|
||||
}
|
||||
|
||||
public void checkOut()
|
||||
{
|
||||
cartContents.clear();
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "Cart content(s): " + String.join(", ", cartContents);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
class ShoppingCartTest
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
ShoppingCart cart = new ShoppingCart();
|
||||
System.out.println(cart);
|
||||
cart.addToCart("foo");
|
||||
cart.addToCart("bar");
|
||||
cart.addToCart("baz");
|
||||
System.out.println(cart);
|
||||
cart.removeFromCart("bar");
|
||||
System.out.println(cart);
|
||||
cart.checkOut();
|
||||
System.out.println(cart);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
public class Vector
|
||||
{
|
||||
// There's nothing to validate
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public Vector()
|
||||
{
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
public Vector(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
// I feel bad writing this
|
||||
return "(" + x + ", " + y + ")";
|
||||
}
|
||||
|
||||
public Vector add(Vector other)
|
||||
{
|
||||
return new Vector(this.x + other.x, this.y + other.y);
|
||||
}
|
||||
|
||||
public Vector subtract(Vector other)
|
||||
{
|
||||
return new Vector(this.x - other.x, this.y - other.y);
|
||||
}
|
||||
|
||||
public Vector multiply(Vector other)
|
||||
{
|
||||
// instruction unclear, applying element-wise multiplication
|
||||
return new Vector(this.x * other.x, this.y * other.y);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
class VectorTest
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
Vector u = new Vector(4, 20);
|
||||
Vector v = new Vector(6, 9);
|
||||
System.out.printf("u = %s\nv = %s\nu + v = %s\nu - v = %s\nu * v = %s\n",
|
||||
u, v, u.add(v), u.subtract(v), u.multiply(v));
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,21 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static java.util.Collections.swap;
|
||||
|
||||
class Stats
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
var numbers = new ArrayList<Double>();
|
||||
for (int i = 0; i < n; ++i)
|
||||
numbers.add(scanner.nextDouble());
|
||||
for (int m = 0; n > 1; n = m, m = 0)
|
||||
for (int i = 1; i < n; ++i)
|
||||
if (numbers.get(i).compareTo(numbers.get(i - 1)) < 0)
|
||||
swap(numbers, m = i, i - 1);
|
||||
System.out.println(numbers);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class Centriod
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
double m = 0.0, x = 0.0, y = 0.0;
|
||||
|
||||
while (scanner.hasNextDouble())
|
||||
{
|
||||
double n = scanner.nextDouble();
|
||||
if (!scanner.hasNextDouble())
|
||||
break;
|
||||
double s = scanner.nextDouble();
|
||||
if (!scanner.hasNextDouble())
|
||||
break;
|
||||
double i = scanner.nextDouble();
|
||||
|
||||
m += n;
|
||||
x += n * s;
|
||||
y += n * i;
|
||||
}
|
||||
System.out.println(m + " " + x/m + " " + y/m);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class Closest
|
||||
{
|
||||
private static double x, y, z;
|
||||
|
||||
private static double dist(double a, double b, double c)
|
||||
{
|
||||
double g = x - a;
|
||||
double h = y - b;
|
||||
double i = z - c;
|
||||
return g*g + h*h + i*i;
|
||||
}
|
||||
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
Double length = null;
|
||||
double a = 0.0, b = 0.0, c = 0.0;
|
||||
|
||||
x = Double.parseDouble(args[0]);
|
||||
y = Double.parseDouble(args[1]);
|
||||
z = Double.parseDouble(args[2]);
|
||||
while (scanner.hasNextDouble())
|
||||
{
|
||||
double d = scanner.nextDouble();
|
||||
if (!scanner.hasNextDouble())
|
||||
break;
|
||||
double e = scanner.nextDouble();
|
||||
if (!scanner.hasNextDouble())
|
||||
break;
|
||||
double f = scanner.nextDouble();
|
||||
double len = dist(d, e, f);
|
||||
|
||||
if (length == null || len < length)
|
||||
{
|
||||
length = len;
|
||||
a = d;
|
||||
b = e;
|
||||
c = f;
|
||||
}
|
||||
}
|
||||
System.out.println(a + " " + b + " " + c);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
import static java.util.Collections.shuffle;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
class Deal
|
||||
{
|
||||
static final String[] suits = {"clubs", "diamonds", "hearts", "spades"};
|
||||
static final String[] ranks = {"Ace", "Two", "Three", "Four", "Five",
|
||||
"Six", "Seven", "Eight", "Nine", "Ten",
|
||||
"Jack", "Queen", "King"};
|
||||
|
||||
public static void main(String... args)
|
||||
{
|
||||
var deck = range(0, 52).boxed().collect(toList());
|
||||
shuffle(deck);
|
||||
|
||||
// I have no time handling exceptions.
|
||||
int n = Integer.parseInt(args[0]) % 52;
|
||||
while (n-- > 0)
|
||||
{
|
||||
int card = deck.get(n);
|
||||
int suit = card / 13;
|
||||
int rank = card % 13;
|
||||
System.out.printf("%s of %s\n", ranks[rank], suits[suit]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Collections.binarySearch;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
class DiscreteDistro
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var numbers = Stream.of(args).mapToInt(Integer::parseInt)
|
||||
.boxed().collect(toList());
|
||||
int n = numbers.size();
|
||||
for (int i = 1; i < n; ++i)
|
||||
numbers.set(i, numbers.get(i) + numbers.get(i - 1));
|
||||
|
||||
int x = ThreadLocalRandom.current().nextInt(0, numbers.get(n - 1));
|
||||
int i = binarySearch(numbers, x) + 1;
|
||||
System.out.println(numbers);
|
||||
System.out.println(x);
|
||||
System.out.println((i < 0) ? -i : i);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
class Employ
|
||||
{
|
||||
private static Employee scan(Scanner s)
|
||||
{
|
||||
int ID = s.nextInt();
|
||||
s.nextLine();
|
||||
return new Employee(ID, s.nextLine(), s.nextLine(),
|
||||
s.nextDouble(), s.nextDouble());
|
||||
}
|
||||
|
||||
public static void main(String... args) throws FileNotFoundException
|
||||
{
|
||||
var stdin = new Scanner(System.in);
|
||||
var f = new File("employees.txt");
|
||||
var file = new Scanner(f);
|
||||
var employees = new ArrayList<Employee>();
|
||||
int n = stdin.nextInt();
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
employees.add(scan(stdin));
|
||||
employees.add(scan(file));
|
||||
}
|
||||
employees.forEach(System.out::println);
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
public class Employee
|
||||
{
|
||||
private int ID;
|
||||
private String name;
|
||||
private String dep;
|
||||
private double basic;
|
||||
private double extra;
|
||||
|
||||
public Employee(int ID, String name, String dep, double basic, double extra)
|
||||
{
|
||||
this.ID = ID;
|
||||
this.name = name;
|
||||
this.dep = dep;
|
||||
this.basic = basic;
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
public int getID() { return ID; }
|
||||
public String getName() { return name; }
|
||||
public String getDep() { return dep; }
|
||||
public double getIncome() { return basic + extra*2.5; }
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return String.format("#%d %s [%s]: %g", ID, name, dep, getIncome());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
class HowMany
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
System.out.println(args.length);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class LongestRun
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
Integer number = null;
|
||||
Integer length = null;
|
||||
Integer n = null, len = null;
|
||||
|
||||
while (scanner.hasNextInt())
|
||||
{
|
||||
Integer i = scanner.nextInt();
|
||||
if (i == n)
|
||||
{
|
||||
len++;
|
||||
continue;
|
||||
}
|
||||
if (length == null || len > length)
|
||||
{
|
||||
number = n;
|
||||
length = len;
|
||||
}
|
||||
n = i;
|
||||
len = 1;
|
||||
}
|
||||
System.out.printf("Longest run: %d consecutive %d\n", number, length);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static java.util.Collections.min;
|
||||
import static java.util.Collections.max;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
class MaxMin
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var numbers = new ArrayList<Integer>();
|
||||
var scanner = new Scanner(System.in);
|
||||
while (scanner.hasNextInt())
|
||||
numbers.add(scanner.nextInt());
|
||||
System.out.printf("Min: %d\nMax: %d\n", min(numbers), max(numbers));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
class Stats
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
var numbers = new ArrayList<Double>();
|
||||
for (int i = 0; i < n; ++i)
|
||||
numbers.add(scanner.nextDouble());
|
||||
|
||||
double m = numbers.stream().mapToDouble(Double::doubleValue).sum() / n;
|
||||
double s = Math.sqrt(numbers.stream()
|
||||
.mapToDouble(x -> Math.pow(x - m, 2)).sum() / n);
|
||||
System.out.printf("Mean: %f\nStandard deviation: %f\n", m, s);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
class Transpose
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
int[][] m = {{7, 8, 9},
|
||||
{4, 5, 6},
|
||||
{1, 2, 3}};
|
||||
int n = 3; // some sort of abstraction
|
||||
System.out.println("Original matrix:");
|
||||
for (int i = 0; i < n; ++i)
|
||||
System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
|
||||
|
||||
for (int i = 1; i < n; ++i)
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
m[i][j] ^= m[j][i];
|
||||
m[j][i] ^= m[i][j];
|
||||
m[i][j] ^= m[j][i];
|
||||
}
|
||||
System.out.println("Transposed matrix:");
|
||||
for (int i = 0; i < n; ++i)
|
||||
System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class WordCount
|
||||
{
|
||||
public static void main(String... args)
|
||||
{
|
||||
var scanner = new Scanner(System.in);
|
||||
int count = 0;
|
||||
while (scanner.hasNext())
|
||||
{
|
||||
scanner.next();
|
||||
count++;
|
||||
}
|
||||
System.out.println(count);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue