initial solutions

This commit is contained in:
2026-01-30 01:44:23 +01:00
parent 9f58e12d48
commit a732c16c12
16 changed files with 540 additions and 2 deletions

3
.gitignore vendored
View File

@@ -14,4 +14,5 @@ target/
out/
# JDT-specific (Eclipse Java Development Tools)
# .classpath
.classpath
.project

61
flake.lock generated Normal file
View File

@@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1768127708,
"narHash": "sha256-1Sm77VfZh3mU0F5OqKABNLWxOuDeHIlcFjsXeeiPazs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ffbc9f8cbaacfb331b6017d5a5abb21a492c9a38",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

35
flake.nix Normal file
View File

@@ -0,0 +1,35 @@
{
description = "A java self hate devShell";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
overlays = [ ];
pkgs = import nixpkgs {
inherit system overlays;
};
in
{
devShells.default =
with pkgs;
mkShell {
buildInputs = [
maven
jdk25_headless
];
JAVA_HOME = "${pkgs.jdk25_headless}/lib/openjdk";
};
}
);
}

View File

@@ -24,6 +24,11 @@
<artifactId>junit-jupiter</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.5.0-jre</version>
</dependency>
</dependencies>
<build>
@@ -52,4 +57,4 @@
</plugin>
</plugins>
</build>
</project>
</project>

View File

@@ -7,4 +7,5 @@ open module ovinger {
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires com.google.common;
}

View File

@@ -0,0 +1,4 @@
package oving1;
public class Account {
}

View File

@@ -0,0 +1,64 @@
package oving1;
public class LineEditor {
String text = "";
int insertionIndex = 0;
LineEditor() {
}
void left() {
insertionIndex -= 1;
}
void right() {
insertionIndex += 1;
}
void insertString(String s) {
String a = text.substring(0, insertionIndex - 1);
String b = text.substring(insertionIndex, text.length());
text = a + s + b;
}
void deleteLeft() {
if (insertionIndex < 1) {
return;
}
String a = text.substring(0, insertionIndex - 2);
String b = text.substring(insertionIndex, text.length());
text = a + b;
}
void deleteRight() {
if (insertionIndex >= text.length() - 1) {
return;
}
String a = text.substring(0, insertionIndex - 1);
String b = text.substring(insertionIndex + 1, text.length());
text = a + b;
}
String getText() {
return text;
}
void setText(String s) {
text = s;
}
int getInsertionIndex() {
return insertionIndex;
}
void setInsertionIndex(int i) {
insertionIndex = i;
}
@Override
public String toString() {
String a = text.substring(0, insertionIndex - 1);
String b = text.substring(insertionIndex, text.length());
return a + "|" + b;
}
}

View File

@@ -0,0 +1,4 @@
package oving1;
public class Location {
}

View File

@@ -0,0 +1,6 @@
// class Main {
// public static void main(String[] args) {
// System.out.println("Hello, World!");
// LineEditor line_editor = new LineEditor("test", 0);
// }
// }

View File

@@ -0,0 +1,87 @@
package oving1;
public class Rectangle {
int x1;
int y1;
int x2;
int y2;
public Rectangle(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
int getMinX() {
return Math.min(x1, x2);
}
int getMinY() {
return Math.min(y1, y2);
}
int getMaxX() {
return Math.max(x1, x2);
}
int getMaxY() {
return Math.max(y1, y2);
}
int getWidth() {
return getMaxX() - getMinX();
}
int getHeight() {
return getMaxY() - getMinY();
}
boolean isEmpty() {
return getWidth() == 0 || getHeight() == 0;
}
boolean contains(int x, int y) {
return x >= getMinX() && x <= getMaxX() && y >= getMinY() && y <= getMaxY();
}
boolean contains(Rectangle rect) {
return contains(rect.x1, rect.y1) && contains(rect.x2, rect.y1) && contains(rect.x1, rect.y2)
&& contains(rect.x2, rect.y2);
}
boolean add(int x, int y) {
int px1 = x1;
int px2 = x2;
int py1 = y1;
int py2 = y2;
x1 = Math.min(x, x1);
x2 = Math.max(x, x2);
y1 = Math.min(y, y1);
y2 = Math.max(y, y2);
return px1 == x1 && px2 == x2 && py1 == y1 && py2 == y2;
}
boolean add(Rectangle rect) {
return add(rect.x1, rect.y1) || add(rect.x2, rect.y1) || add(rect.x1, rect.y2) || add(rect.x2, rect.y2);
}
Rectangle union(Rectangle rect) {
int px1 = x1;
int px2 = x2;
int py1 = y1;
int py2 = y2;
add(rect);
Rectangle r = new Rectangle(x1, y1, x2, y2);
x1 = px1;
x2 = px2;
y1 = py1;
y2 = py2;
return r;
}
@Override
public String toString() {
return "";
}
}

View File

@@ -0,0 +1,63 @@
package oving1;
public class StopWatch {
boolean active = false;
int time = 0;
int ticks = 0;
int lapTime = 0;
int lap = 0;
int lastLapTime = -1;
public StopWatch() {
}
boolean isStarted() {
return active;
}
boolean isStopped() {
return !active;
}
int getTicks() {
return ticks;
}
int getTime() {
return time;
}
int getLapTime() {
return lapTime;
}
int getLastLapTime() {
return lastLapTime;
}
void tick(int ticks) {
this.ticks += ticks;
if (isStopped()) {
return;
}
}
void start() {
active = true;
}
void lap() {
lap += 1;
lastLapTime = lapTime;
lapTime = 0;
}
void stop() {
active = false;
}
@Override
public String toString() {
return "";
}
}

View File

@@ -0,0 +1,4 @@
package oving1;
public class UpOrDownCounter {
}

View File

@@ -0,0 +1,7 @@
package oving2;
class Main {
public static void main(String[] args) {
Person p = new Person();
}
}

View File

@@ -0,0 +1,119 @@
package oving2;
import java.util.Date;
import com.google.common.net.InternetDomainName;
public class Person {
private String name;
private String email;
private Date birthday;
private char gender;
private String SSN;
public Person() {
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public Date getBirthday() {
return birthday;
}
public char getGender() {
return gender;
}
public String getSSN() {
return SSN;
}
public void setName(String name) {
int i = name.indexOf(' ');
if (i == -1) {
throw new IllegalArgumentException("Did not find both prename and surname.");
}
String prename = name.substring(0, i);
String surname = name.substring(i + 1, name.length());
if (prename.contains(" ") || surname.contains(" ")) {
throw new IllegalArgumentException("There should only be one space which separates prename and surname.");
}
if (prename.length() < 2 || surname.length() < 2) {
throw new IllegalArgumentException("Prename or surname should not be less than 2 characters.");
}
if (!isAlphabetic(prename) || !isAlphabetic(surname)) {
throw new IllegalArgumentException("Prename and surname should only contain letters.");
}
this.name = name;
}
private boolean isAlphabetic(String s) {
char[] chars = name.toCharArray();
for (char c : chars) {
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
public void setEmail(String email) {
if (email != null) {
throw new IllegalArgumentException("Email is already set.");
}
if (name == null) {
throw new IllegalArgumentException("Name needs to be set before email.");
}
int i = email.indexOf('@');
if (i == -1) {
throw new IllegalArgumentException("Email should contain 1 @");
}
String[] names = name.split(" ");
String first = email.substring(0, i);
String second = email.substring(i + 1, email.length());
if (first.contains("@") || second.contains("@")) {
throw new IllegalArgumentException("Email should contain 1 @");
}
if (first != names[0] + "." + names[1]) {
throw new IllegalArgumentException("Email username should be on the form: \"prename.surname\".");
}
boolean isDomain = true;
try {
InternetDomainName d = InternetDomainName.from(second);
isDomain = d.hasPublicSuffix();
} catch (IllegalArgumentException e) {
isDomain = false;
}
if (!isDomain) {
throw new IllegalArgumentException("Email domain is not on the form: \"local.domain\"");
}
this.email = email;
}
void setBirthday(Date date) {
Date now = new Date();
if (now.before(date)) {
throw new IllegalArgumentException("Birthday cannot be set to a future date.");
}
birthday = date;
}
void setGender(char c) {
if (c != 'M' && c != 'F' && c != '\0') {
throw new IllegalArgumentException("Invalid gender value.");
}
gender = c;
}
void setSSN(String s) {
}
}

View File

@@ -0,0 +1,65 @@
package oving2;
public class Vehicle {
private char vehicleType;
private char fuelType;
private String registrationNumber;
Vehicle(char vehicleType, char fuelType, String regstrationNumber) {
setVehicleType(vehicleType);
setFuelType(fuelType);
setRegistrationNumber(registrationNumber);
}
char getVehicleType() {
return vehicleType;
}
char getFuelType() {
return fuelType;
}
String getRegistrationNumber() {
return registrationNumber;
}
void setVehicleType(char vehicleType) {
if (vehicleType != 'M' && vehicleType != 'C') {
throw new IllegalArgumentException("Invalid vehicle type, found: \"" + vehicleType + "\"");
}
this.vehicleType = vehicleType;
}
void setFuelType(char fuelType) {
if (fuelType != 'H' && fuelType != 'E' && fuelType != 'D' && fuelType != 'G') {
throw new IllegalArgumentException("Invalid vehicle type, found: \"" + fuelType + "\"");
}
this.fuelType = vehicleType;
}
void setRegistrationNumber(String registrationNumber) {
String code = registrationNumber.substring(0, 2);
if (fuelType == 'E' && code != "EL" && code != "EK") {
throw new IllegalArgumentException(
"Electric vehicles should have registraition number starting with EL or EK, got: "
+ registrationNumber + "\"");
} else if (fuelType == 'H' && code != "HY") {
throw new IllegalArgumentException(
"Hydrogen vehicles should have registraition number starting with HY, got: " + registrationNumber
+ "\"");
} else if ((fuelType == 'D' || fuelType == 'G') && (code == "EL" || code == "EK" || code == "HY")) {
throw new IllegalArgumentException(
"Diesel or gas vehicles should have reigstartion number not starting with EL, EK, or HY.");
}
if (vehicleType == 'C' && registrationNumber.length() != 7) {
throw new IllegalArgumentException(
"Cars have a registrationNumber of length 7" + registrationNumber + "\"");
}
if (vehicleType == 'M' && registrationNumber.length() != 6) {
throw new IllegalArgumentException(
"Motorbikes have a registrationNumber of length 6" + registrationNumber + "\"");
}
this.registrationNumber = registrationNumber;
}
}

View File

@@ -0,0 +1,12 @@
# Teori
- Hva er en synlighetsmodifikator?
En synlighetsmodifikator endrer på hvem som har tilgang på noe. Enten det er en attributt, metode, eller klasse.
- Hva er forskjellen på private og public og når brukes de?
Hvis man setter en attributt til private vil den kun være direkte tilgjengelig innad i den klassen den blir definert. Hvis den blir satt til public er ikke dette lenger tilfellet og man kan direkte modifisere og lese den utenfor klassen slik som når man har en instans av klassen.
# Oppgaver
## Person
### Del 2
- Navnet kan være delt i fornavn og fullt navn.
- Man kan enten gjøre det slik at man må gi inn begge deler samtidig, eller at man må gi inn det ene, og at man da finner ut av den andre basert på den ene. Man kan og ha at man angir det hver for seg uten at man antar at de er sammmenkoblet.