From 098fee275559a9fe9c6749ed2812a21c77732b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D1=89=D1=83=D0=BA?= Date: Tue, 21 Jul 2020 17:48:05 +0300 Subject: [PATCH 1/2] new dir src/ru/milandr/courses/polishchuk added Alcohol.java, Color.java, Point.java --- .../milandr/courses/polishchuk/Alcohol.java | 153 ++++++++++++++++++ src/ru/milandr/courses/polishchuk/Color.java | 50 ++++++ src/ru/milandr/courses/polishchuk/Point.java | 48 ++++++ 3 files changed, 251 insertions(+) create mode 100644 src/ru/milandr/courses/polishchuk/Alcohol.java create mode 100644 src/ru/milandr/courses/polishchuk/Color.java create mode 100644 src/ru/milandr/courses/polishchuk/Point.java diff --git a/src/ru/milandr/courses/polishchuk/Alcohol.java b/src/ru/milandr/courses/polishchuk/Alcohol.java new file mode 100644 index 0000000..bb81d3b --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/Alcohol.java @@ -0,0 +1,153 @@ +package ru.milandr.courses.polishchuk; + +public class Alcohol { + private String name; + private String manufacturer; + private double degree; + private double volume; + private Color color; + private double transparency; + private double fillPercentage; + private double age; + + Alcohol() { + this.name = null; + this.manufacturer = null; + this.degree = 0.; + this.volume = 0.; + this.color = null; + this.transparency = 0.; + this.fillPercentage = 0.; + this.age = -1; + } + + Alcohol(String name, String manuf, double degree, double volume, Color color, double transparency, double fillPercentage, int age) { + this.name = name; + this.manufacturer = manuf; + this.degree = degree; + this.volume = volume; + this.color = color; + this.transparency = transparency; + this.fillPercentage = fillPercentage; + this.age = age; + } + + Alcohol(String name, String manuf, double degree, double volume) { + this(name, manuf, degree, volume, new Color(255, 255, 255), 1., 100., -1); + } + + public void print() { + System.out.format( + "Name: %s\n" + + "Manufacturer: %s\n" + + "Degree: %.1f\n" + + "Volume (mL): %.3f\n" + + "RGB Color: %X\n" + + "Transparency: %f\n" + + "Fill percentage: %.2f\n", + this.name, + this.manufacturer, + this.degree, + this.volume, + this.color.getRGB(), + this.transparency, + this.fillPercentage + ); + this.printAge(); + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getManufacturer() { + return this.manufacturer; + } + + public void setManufacturer(String mname) { + this.manufacturer = mname; + } + + public double getDegree() { + return this.degree; + } + + public Color getColor() { + return this.color; + } + + public void setColor(Color color) { + this.color = color; + } + + public void setColor(int rgb) { + this.color = new Color(rgb); + } + + public void setColor(int r, int g, int b) { + this.color = new Color(r, g, b); + } + + public double getTransparency() { + return this.transparency; + } + + public void setTransparency(double transp) { + this.transparency = transp; + } + + public double getFillPercentage() { + return this.fillPercentage; + } + + public void setFillPercentage(double percent) { + this.fillPercentage = percent; + } + + public double pourOut(double percent) { + double pouredOut = percent; + + if (percent < 0.) + return 0.; + this.fillPercentage -= percent; + if (this.fillPercentage < 0.) + { + pouredOut = percent + this.fillPercentage; + this.fillPercentage = 0.; + } + return pouredOut; + } + + public boolean isEmpty() { + if (this.fillPercentage == 0.) + return true; + return false; + } + + public void printAge() { + System.out.print("Age: "); + if (this.age < 0) + System.out.println("Undefined"); + else + System.out.format("%d\n", this.age); + } + + public double getAge() { + return this.age; + } + + public void setAge(double age) { + this.age = age; + } + + public void addAge(double years) { + if (years < 0) + return; + this.age += years; + } + +} diff --git a/src/ru/milandr/courses/polishchuk/Color.java b/src/ru/milandr/courses/polishchuk/Color.java new file mode 100644 index 0000000..1cd2f5e --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/Color.java @@ -0,0 +1,50 @@ +package ru.milandr.courses.polishchuk; + +public class Color { + private int rgb; + + Color(int r, int g, int b) { + this.rgb = ((r & 0xff) << 16) | ((g & 0xff) << 8) | b & 0xff; + } + + Color(int rgb) { + this.rgb = rgb; + } + + public int getRGB() { + return this.rgb; + } + + public int getR() { + return (this.rgb & (0xff << 16)) >>> 16; + } + + public int getG() { + return (this.rgb & (0xff << 8)) >>> 8; + } + + public int getB() { + return this.rgb & 0xff; + } + + private void setColorWithShift(int color, int shift) { + if (shift <= 24) + this.rgb = this.rgb & (0xffffffff ^ ((color & 0xff) << shift)); + } + + public void setR(int r) { + this.setColorWithShift(r, 16); + } + + public void setG(int g) { + this.setColorWithShift(g, 8); + } + + public void setB(int b) { + this.setColorWithShift(b, 0); + } + + public void setRBG(int rgb) { + this.rgb = rgb; + } +} diff --git a/src/ru/milandr/courses/polishchuk/Point.java b/src/ru/milandr/courses/polishchuk/Point.java new file mode 100644 index 0000000..5eb5db7 --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/Point.java @@ -0,0 +1,48 @@ +package ru.milandr.courses.polishchuk; + +public class Point { + public double x; + public double y; + + Point(double x, double y) { + this.x = x; + this.y = y; + } + + public double getDistance(Point p) { + return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)); + } + + public void printLineEquation(Point p) { + double k; + + if (this.x == p.x) + System.out.format("x = %f\n", this.x); + else + { + k = (this.y - p.y) / (this.x - this.y); + System.out.format("y = %f * x %+f\n", k, this.y - k * this.x); + } + } + + public Point getMiddle(Point p) + { + return new Point((this.x + p.x) / 2., (this.y + p.y) / 2.); + } + + public void printCircleEquation(Point p) + { + Point middle = this.getMiddle(p); + + System.out.format( + "(x %+.3f)^2 + (y %+.3f)^2 = (%.3f)^2\n", + -middle.x, -middle.y, + this.getDistance(p) / 2. + ); + } + + public void print() + { + System.out.format("(%f, %f)", this.x, this.y); + } +} From e203b2008e4705a4294aa16495ffc59f873fc911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D1=89=D1=83=D0=BA?= Date: Tue, 21 Jul 2020 17:54:38 +0300 Subject: [PATCH 2/2] Alcohol.java added missed setters/getters --- src/ru/milandr/courses/polishchuk/Alcohol.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ru/milandr/courses/polishchuk/Alcohol.java b/src/ru/milandr/courses/polishchuk/Alcohol.java index bb81d3b..a0dff6d 100644 --- a/src/ru/milandr/courses/polishchuk/Alcohol.java +++ b/src/ru/milandr/courses/polishchuk/Alcohol.java @@ -76,6 +76,18 @@ public double getDegree() { return this.degree; } + public void setDegree(double degree) { + this.degree = degree; + } + + public double getVolume() { + return this.volume; + } + + public void setVolume(double volume) { + this.volume = volume; + } + public Color getColor() { return this.color; }