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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package school.lemon.changerequest.java.employees;

/**
* Created by Yaroslav Pavlinskiy on 06.01.2017.
*/
public class Accountant extends Employee {
Accountant(String name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer public constructors everywhere in this task.

super(name);
}

public double calculateSalaryForEmployee(Employee e) {
if(e instanceof Programmer || e instanceof Manager || e instanceof Accountant)
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Using instanceof isn't a good practice and should be avoided if it's possible. I do not see any reason for such check here.
  2. The main idea was to calculate salary for a array/list of employees. They should be passed in constructor/setter method. So we'll have some kind of responsibility. This Accountant is responsible for a list/array of employees and calculates their salary.

{
return e.calculateSalary();
}
return 0;
}
}
25 changes: 25 additions & 0 deletions src/main/java/school/lemon/changerequest/java/employees/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package school.lemon.changerequest.java.employees;

/**
* Created by Yaroslav Pavlinskiy on 06.01.2017.
*/
public class Demo {
public static void main(String[] args) {
Programmer programmer = new Programmer("Stiv");
programmer.setSalary(100);
programmer.working(320);
System.out.println(programmer);

Manager manager = new Manager("Bob");
manager.setSalary(100);
manager.working(320);
System.out.println(manager);

Accountant accountant = new Accountant("Gloria");
accountant.setSalary(100);
accountant.working(320);
System.out.println(accountant);

System.out.println(accountant.calculateSalaryForEmployee(manager));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package school.lemon.changerequest.java.employees;

/**
* Created by Yaroslav Pavlinskiy on 06.01.2017.
*/
public abstract class Employee {
public final int HOURS_PER_MONTH = 160;
private String name;
private double salary;
private int hours;

Employee(String name)
{
this.name = name;
}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}

public int getHours() {
return hours;
}

public void setSalary(double salary)
{
this.salary = salary;
}

public void working(int hours)
{
this.hours += hours;
}

public double calculateSalary()
{
if(this.hours >= HOURS_PER_MONTH)
return salary;
return salary/(HOURS_PER_MONTH/hours);
}

@Override
public String toString() {
return String.format("%s with salary %s $ worked %s hours -> salary %s $",getName(),getSalary(),getHours(),calculateSalary());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package school.lemon.changerequest.java.employees;

/**
* Created by Yaroslav Pavlinskiy on 06.01.2017.
*/
public class Manager extends Employee {
Manager(String name) {
super(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package school.lemon.changerequest.java.employees;

/**
* Created by Yaroslav Pavlinskiy on 06.01.2017.
*/
public class Programmer extends Employee {
Programmer(String name) {
super(name);
}

@Override
public double calculateSalary() {
return (double)getHours()/((double)HOURS_PER_MONTH / getSalary());
}
}
104 changes: 104 additions & 0 deletions src/main/java/school/lemon/changerequest/java/matrix/Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package school.lemon.changerequest.java.matrix;

/**
* Created by Yaroslav Pavlinskiy on 12.12.2016.
*/
public class Matrix {

private final int Rows; // count Rows
Copy link
Contributor

Choose a reason for hiding this comment

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

Please follow java code convention. All fields, methods, method arguments should be named in lowerCamelCase style.

private final int Columns; // count Columns
private final int[][] data; // 2d array for matrix

public Matrix(int R,int C)// Constructor with Rows and Columns
{
this.Rows = R;
this.Columns = C;
this.data = new int[R][C];
}

public Matrix(int[][]data) //Constructor 2d array
{
this.Rows = data.length;
this.Columns = data[0].length;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's possible to create multidimensional arrays in Java like:

        int[][] example = new int[3][];
        example[0] = new int[100];
        example[1] = new int[1000];
        example[2] = new int[0];

So determine columns count by the data[0].length is not the best approach. Please add verification that all rows has the same columns number.

By the way, if you will modify this incoming array - your matrix will be changed too. So I'd prefer copying.

this.data = data;
}
private Matrix(Matrix A) { this(A.data); }

public Matrix AddMatrix(Matrix B) // Method that adding matrix A and B
{
return Calculate(B,'+');
}

public Matrix SubtractMatrix(Matrix B)
{
return Calculate(B,'-');
}

public Matrix MultiplyNumber(int n)
{
Matrix A = this;
Matrix C = new Matrix(Rows, Columns);
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
C.data[i][j] = A.data[i][j] * n;
return C;
}

public Matrix MultiplyMatrix(Matrix B)
{
return Calculate(B,'*');
}

public Matrix Transpose()
{
Matrix A = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure you need extract this to variable. As for me it's even better to use data directly. Like C.data[i][j] = data[j][i];

Matrix C = new Matrix(Rows,Columns);
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
C.data[i][j] = A.data[j][i];
return C;
}

public Matrix Calculate(Matrix B,char d)
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. There is no need in this method at all. Determining operation by char is not the good approach, because you now which method was called and which operation to execute. So please split your logic to specific methods.
  2. Not sure multiplication is done correctly. Please read about it https://en.wikipedia.org/wiki/Matrix_multiplication
  3. As you may remember the point was that current matrix should be changed. So no need to create third matrix. You should modify existing one.

{
Matrix A = this;
if (B.Rows != A.Rows || B.Columns != A.Columns) return null;
Matrix C = new Matrix(Rows, Columns);
switch (d)
{
case '*':
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
C.data[i][j] = A.data[i][j] * B.data[i][j];
break;
case '+':
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
C.data[i][j] = A.data[i][j] + B.data[i][j];
break;
case '-':
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
C.data[i][j] = A.data[i][j] - B.data[i][j];
break;

}

return C;
}

@Override
public String toString() {
for(int i = 0; i < Rows;i++)
Copy link
Contributor

Choose a reason for hiding this comment

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

The main point of toString method that it should return string representation of the object and do not print anything.
So please use StringBuilder or any other way to create resulting string and get rid of System.out.print.

{
for(int j = 0; j < Columns;j++)
{
System.out.print(data[i][j]+" ");
}
System.out.println();
}
System.out.println("------------");
return "";
}

}
52 changes: 52 additions & 0 deletions src/main/java/school/lemon/changerequest/java/matrix/TestApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package school.lemon.changerequest.java.matrix;
import java.io.*;
/**
* Created by Yaroslav Pavlinskiy on 12.12.2016.
*/
public class TestApp {

public static void main(String[] args) {
TestApp app = new TestApp();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need this object?


int[][]A = new int[2][2];//initialization matrix A
int[][]B = new int[2][2];//initialization matrix

A[0][0] = 4;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer int[][] example = {{1,2}, {3,4}}; such kind of initialization.

A[0][1] = 2;
A[1][0] = 9;
A[1][1] = 0;

B[0][0] = 3;
B[0][1] = 1;
B[1][0] = -3;
B[1][1] = 4;

Matrix a = new Matrix(A);
System.out.println("Matrix A:");
a.toString();

Matrix b = new Matrix(B);
System.out.println("Matrix B:");
b.toString();

System.out.println("Matrix Add:");
a.AddMatrix(b).toString();

System.out.println("Matrix Subtract:");
a.SubtractMatrix(b).toString();

System.out.println("MultiplayNumber:");
a.MultiplyNumber(2).toString();

System.out.println("MultiplayMatrix:");
a.MultiplyMatrix(b).toString();

System.out.println("Transpose Matrix:");
a.Transpose().toString();





}
}