Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions src/ru/milandr/courses/polishchuk/Alcohol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
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;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мы обычно не выравниваем поля по форматированию. Достаточно поставить пробел между типом и именем поля.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще есть волшебное сочетание ctrl alt L в idea, которое тебе выровняет весь код в текущем файле. Можно также применять, выбрав пакет - как результат получим форматирование по всем файлам в текущем пакете и вложенных.


Alcohol() {
this.name = null;
this.manufacturer = null;
this.degree = 0.;
this.volume = 0.;
this.color = null;
this.transparency = 0.;
this.fillPercentage = 0.;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Никогда не видел такой конструкции, сходу не нагуглил. Что она означает?

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) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А ты конструкторы намеренно в паблик не выводишь?

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;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В геттерах можно this не использовать

}

public void setName(String name) {
this.name = name;
}

public String getManufacturer() {
return this.manufacturer;
}

public void setManufacturer(String mname) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно стараются в качестве имени аргумента в сеттере использовать такое же имя, как и у поля.

this.manufacturer = mname;
}

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;
}

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.;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Минутка занудства. Есть рекомендация любые операции в циклах, ифах, элсах заключать в фигурные скобки, чтобы при внесении изменений не написать код мимо структурного блока.

this.fillPercentage -= percent;
if (this.fillPercentage < 0.)
{

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Скобочка по-сишному проскочила. Мы ставим на той же строке.

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;
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно геттеры-сеттеры идут в начале класса, а остальные методы - после них


public void addAge(double years) {
if (years < 0)
return;
this.age += years;
}

}
50 changes: 50 additions & 0 deletions src/ru/milandr/courses/polishchuk/Color.java
Original file line number Diff line number Diff line change
@@ -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;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно без нужды мы не занимаемся разрядными сдвигами, побитовыми операции и тп. Здорово, что ты это знаешь, без шуток, но такие конструкции тяжело читать, а в масштабах работы всего приложения (как правило, большого) выигрыш они дают пшиковый.

}

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;
}
}
48 changes: 48 additions & 0 deletions src/ru/milandr/courses/polishchuk/Point.java
Original file line number Diff line number Diff line change
@@ -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);
}
}