This commit is contained in:
Your Name
2023-09-27 23:10:58 +02:00
parent 18efec8bb6
commit 1f7befc099
15 changed files with 253 additions and 8 deletions

View File

@@ -0,0 +1,32 @@
package P1;
public class Circle implements Geometry {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public double berechneUmfang() {
return 2*Math.PI*radius;
}
@Override
public double berechneFlaeche() {
return Math.pow(radius,2)*Math.PI;
}
public String toString(){
return "Radius: " + radius;
}
}

View File

@@ -0,0 +1,6 @@
package P1;
public interface Geometry {
double berechneUmfang();
double berechneFlaeche();
}

View File

@@ -0,0 +1,10 @@
package P1;
public class Main {
public static void main(String[] args) {
var rec1 = new Rectangle(11,20);
var rec2 = new Rectangle(10,20);
System.out.println(rec1.equals(rec2));
System.out.println(rec1.toString()+" "+ rec2.toString());
}
}

View File

@@ -0,0 +1,65 @@
package P1;
import java.util.Objects;
public class Person {
private final String name;
private final String city;
private String street;
private String zipcode;
public Person(final String name, final String city, String street, String zipcode) {
Objects.requireNonNull(name, "NOT NULL");
Objects.requireNonNull(city, "NOT NULL");
this.name = name;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
// public boolean equals(Person input) {
// if (!name.equals(input.name))
// return false;
// if (!city.equals(input.city))
// return false;
// if (!street.equals(input.street))
// return false;
// if (!zipcode.equals(input.zipcode))
// return false;
// return true;
// }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(city, person.city) && Objects.equals(street, person.street) && Objects.equals(zipcode, person.zipcode);
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}

View File

@@ -0,0 +1,55 @@
package P1;
public class Rectangle implements Geometry{
private int breite;
private int hoehe;
public Rectangle(int breite, int hoehe) {
this.breite = breite;
this.hoehe = hoehe;
}
public int getBreite() {
return breite;
}
public void setBreite(int breite) {
this.breite = breite;
}
public int getHoehe() {
return hoehe;
}
public void setHoehe(int hoehe) {
this.hoehe = hoehe;
}
public boolean equals(Rectangle input){
if(breite==input.breite && hoehe == input.hoehe){
return true;
}
return false;
}
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + hoehe; result = prime * result + breite;
return result;
}
public String toString(){
return "breite: " + breite + "hoehe: " + hoehe+"hash: " + hashCode();
}
@Override
public double berechneUmfang() {
return 2*breite+2*hoehe;
}
@Override
public double berechneFlaeche() {
return hoehe * breite;
}
}

View File

@@ -0,0 +1,29 @@
package P1;
import java.util.Objects;
public class RefinedPerson extends Person{
private String nickName;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public RefinedPerson(String name, String city, String street, String zipcode) {
super(name, city, street, zipcode);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RefinedPerson that = (RefinedPerson) o;
return Objects.equals(nickName, that.nickName);
}
}

View File

@@ -0,0 +1,22 @@
package P1;
public class Test {
public static void main(String[] args) {
Geometry[] rec = {
new Rectangle(10,10),
new Rectangle(10,10),
new Rectangle(10,10),
new Rectangle(10,10),
new Circle(10),
new Circle(10),
new Circle(10),
new Circle(10)};
for(Geometry g:rec){
System.out.println("Flache: " +g.berechneFlaeche());
System.out.println("Umfang: "+ g.berechneUmfang());
System.out.println("String: "+ g.toString());
}
}
}

View File

@@ -0,0 +1,14 @@
package P1;
public class Test2 {
public static void main(String[] args) {
var p1 = new Person("a","a","a","a");
var p2 = new Person("a","a","a","b");
var rp1 = new RefinedPerson("a","a","a","b");
rp1.setNickName("bob");
var rp2 = new RefinedPerson("a","a","a","b");
rp2.setNickName("bob");
System.out.println(p1.equals(p2));
System.out.println(rp1.equals(rp2));
}
}