-
Notifications
You must be signed in to change notification settings - Fork 8
Task with matrix done. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
| super(name); | ||
| } | ||
|
|
||
| public double calculateSalaryForEmployee(Employee e) { | ||
| if(e instanceof Programmer || e instanceof Manager || e instanceof Accountant) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| return e.calculateSalary(); | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| 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()); | ||
| } | ||
| } |
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible to create multidimensional arrays in Java like: So determine columns count by the 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| 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++) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main point of |
||
| { | ||
| for(int j = 0; j < Columns;j++) | ||
| { | ||
| System.out.print(data[i][j]+" "); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| System.out.println("------------"); | ||
| return ""; | ||
| } | ||
|
|
||
| } | ||
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer |
||
| 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(); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
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.