Skip to content
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

HW1_done #14

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 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
Expand Up @@ -3,6 +3,7 @@

public class Task1 {
public static double calculateP(double a) {
return 0;

return 4 * a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
public class Task10 {

public static boolean containDigitTwo(int n) {

while (n > 0) {
if (n % 10 == 2) {
return true;
}
n = n / 10;

}
Copy link
Member

Choose a reason for hiding this comment

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

It's not correct solution.
It'll loop forever for some numbers, for example, when n=27:

  1. n>0 -> true -> go into while loop
  2. n%10 == 2 -> false -> do not divide n by 10 -> go to step 1.


return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

public class Task2 {
public static int getMeters(int centimeters) {
return 0;

return centimeters / 100;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

public class Task3 {
public static boolean isBetween(int a, int b, int c) {
return false;

return ((a <= b && b <= c) || (a >= b && b >= c));

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
public class Task4 {

public static double getSumOfGreatest(double a, double b, double c) {
return 0;
}
}
if (a < b && b < c) {
return c + b;
}
if (a > b && b > c) {
return b + a;
}
else {
return a + c;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Please format you code before commit, you can use IDEA shortcut: CTRL+ALT+L.

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

public class Task5 {
public static double calculateA(double x, double y, double z) {
return 0;
return ((2 * Math.sin(x - Math.PI / 6) * calculateB(z)) / (1 / 2.0 + (Math.pow(Math.sin(y), 2))));
Copy link
Member

Choose a reason for hiding this comment

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

It's quite hard to read such expressions. It'll be easier for understanding if you'll split it into some pretty parts, like top-part of expression, bottom-part of expression.

But, anyway, it do pass all the tests.

}

private static double calculateB(double z) {
return 0;
return 1 + (Math.pow(z, 2)) / (3 + ((Math.pow(z, 2)) / 5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

public class Task6 {
public static double calculateS(double x) {
return 0d;
return (1 + x + (Math.pow(x, 2)/factorial(2)) + (Math.pow(x, 3)/factorial(3)) + (Math.pow(x, 4)/factorial(4)));

}

private static int factorial(int n) {
for (int i = 1; i <= n; i++) {
n = n * i;
}
return n;
Copy link
Member

Choose a reason for hiding this comment

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

Your factorial function doesn't return correct result.
Try to calculate on your own with n=2:

  1. i=1; n=2; i>=n -> true; n=n*i; i++
  2. i=2; n=2; i>=n -> true; n=n*i; i++
  3. i=3; n=4; i>=n -> true; n=.....

As you see - you'll loop until you'll get overflow on int value and number will be negative.

}

public static double calculateZ(double x, double y) {
return 0d;

return Math.sin(Math.pow(x, 3)) + Math.pow(Math.cos(y), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@

public class Task7 {
public static String getDayTitle(int dayIndex) {
return "";

switch (dayIndex)
{
case 1:
return "Monday";

case 2:
return "Tuesday";

case 3:
return "Wednesday";

case 4:
return "Thursday";

case 5:
return "Friday";

case 6:
return "Saturday";

case 7:
return "Sunday";

default:
return "Error";
Copy link
Member

Choose a reason for hiding this comment

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

you should return error in lowercase: "error"

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class Task8 {
public static int calculateSum(int N) {
return 0;
int result = 0;
int i;
for (i = N; 2 * N >= i; i++)
result = (int) (i + Math.pow((2 * N), 2));
Copy link
Member

Choose a reason for hiding this comment

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

Check task description again.
You should calculate: N^2 + (N+1)^2 + (N+2)^2+ ... + (N+N)^2

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
public class Task9 {

public static boolean isPowerOfThree(int n) {
return false;
}

if (n < 1){
return false;
}

while (n % 3 == 0) {
n = n / 3;
}
return true;
Copy link
Member

Choose a reason for hiding this comment

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

This implementation doesn't work.
You need to re-assign value of n/3 back to n, cause right now you'll have infinite loop in many cases.

Copy link
Member

Choose a reason for hiding this comment

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

Check again that your solution do pass all the given test cases.
For example, if n=15 you'll return true, while 15 is not power of 3.

}
}