Skip to main content

public class Sensor

LAB2

ex1
public class Sensor {
    int value;

    public Sensor()
    {
        this.value = -1;
    }


    public void change(int k) {

        this.value = k;
    }

    public String toString() {
        String value = Integer.toString(this.value);
        return value;
    }

}



public class TestSensor {
    public static void main(String[] args) {
        Sensor s1 = new Sensor();
        System.out.println("Sensor s1 value = " + s1.toString());
        Sensor s2 = new Sensor();
        System.out.println("Sensor s2 value = " + s2.toString());
        s2.change(374);
        System.out.println("Sensor s2 value = " + s2.toString());


    }
}


ex2

public class Circle {
    private double radius;
    private String color;

    public Circle() {
        this.radius = 1.0;
        this.color = "red";
    }

    Circle(double r) {
        radius = r;
        color = "pink";
    }

    Circle(double r, String c) {
        radius = r;
        color = c;
    }

    public double getRadius() {

        return radius;
    }

    public double getArea() {
        return 2 * Math.PI * radius;
    }
}


public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle();
        System.out.println("The first circle has the length of the radius: " + c1.getRadius());
        Circle c2 = new Circle();
        System.out.println("The second circle has the area: " + c2.getArea());
    }
}


ex3

public class Author {
    private String name;
    private String email;
    private char gender;

    public Author(String name, String email, char gender) {
        this.name = name;
        this.email = email;
        this.gender = gender;
    }

    public String getName() {

        return name;
    }

    public String getEmail() {

        return email;
    }

    public char getGender() {

        return gender;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String toString() {
        String v = this.name + "(" + this.gender + ") at " + this.email;
        return v;
    }
}


public class TestAuthor {
    public static void main(String[] args) {
        Author a1 = new Author("Ana Popescu", "anapopescu@yahoo.com", 'f');
        Author a2 = new Author("Alexandru Rus", "alexandru.rus@yahoo.com",'m');
        Author a3 = new Author("Maria Campean", "mariacampean12@yahoo.com", 'f');
        System.out.println("The name of the first author is "+ a1.getName());
        System.out.println("The gender of the second author is "+ a2.getGender());
        System.out.println("The email of the third author is "+ a3.getEmail());
        String s = a1.toString();
        System.out.println("First author: "+s);
        a2.setEmail("alexandru_rus@gmail.com");
        System.out.println("The new email of the second author is "+a2.getEmail());
    }
}



ex4

public class MyPoint {
    public int x, y;

    public MyPoint() {
        this.x = 0;
        this.y = 0;
    }

    public MyPoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setXY(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String toString() {
        String coordinates = "(" + Integer.toString(this.x) + ", " + Integer.toString(this.y) + ")";
        return coordinates;
    }

    public double distance(int x, int y)
    {
        return Math.sqrt((this.x - x) ^ 2 + (this.y - y) ^ 2);
    }

    public double distance(MyPoint p) {

        return Math.sqrt((this.x - p.x) ^ 2 + (this.y - p.y) ^ 2);
    }
}


public class TestMyPoint {
    public static void main(String[] args) {
        MyPoint p1 = new MyPoint();
        MyPoint p2 = new MyPoint(3, 6);
        MyPoint p3 = new MyPoint(2, 5);
        MyPoint p4 = new MyPoint(4, 4);
        String a = p1.toString();
        String b = p2.toString();
        String c = p3.toString();
        String d = p4.toString();
        System.out.println("a=" + a);
        System.out.println("b=" + b);
        System.out.println("c=" + c);
        System.out.println("d=" + d);
        p1.setX(9);
        p4.setY(1);
        System.out.println("The new X coordinate for p1 is: " + p1.getX());
        System.out.println("The new Y coordinate for p4 is: " + p4.getY());
        p2.setXY(1, 2);
        System.out.println("The new coordinates for p2 are: (" + p2.getX() + ", " + p2.getY() + ")");
        System.out.println("The distance between p3 and (0,0) is: " + p3.distance(0, 0));
        System.out.println("The distance between p1 and p2 is:  " + p1.distance(p2));
    }
}