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
@@ -1,9 +1,98 @@
package school.lemon.changerequest.java.introduction.pr1;


import java.util.Scanner;

public class ConsoleCalculator {

public static void main(String[] args) {
System.out.println("Console calculator:");
System.out.println("Enter 'add' to perform addition.");
System.out.println("Enter 'sub' to perform subtraction.");
System.out.println("Enter 'mul' to perform multiplication.");
System.out.println("Enter 'div' to perform division.");
System.out.println("Enter 'exit' to exit.");
System.out.println("Enter 'help' to see help message.");
boolean start = true;
while (start) {
System.out.println("Make your choice.");
Scanner sc = new Scanner(System.in);
Copy link

Choose a reason for hiding this comment

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

Creating scanner in each iteration is not a good idea.

String choice = sc.nextLine();
choice.toLowerCase();
Copy link

Choose a reason for hiding this comment

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

It's not working properly because string is immutable. Please read documentation about string methods.

switch (choice) {
case "add":
add();
break;
case "sub":
sub();
break;
case "mul":
mul();
break;
case "div":
div();
break;
case "exit":
exit();
break;
case "help":
help();
break;
default:
Copy link

Choose a reason for hiding this comment

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

I'd prefer print help message by default.

}
}


}

private static void help() {
Copy link

Choose a reason for hiding this comment

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

What about help message?

}

private static void exit() {
System.out.println("Bye-bye");
System.exit(0);
Copy link

Choose a reason for hiding this comment

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

It's a bad practice to use System.exit(). Please get rid of it.

}

private static void div() {
Copy link

Choose a reason for hiding this comment

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

There are too many duplicated code... Reading two integers for example. Maybe you can get rid of it?

Scanner sc = new Scanner(System.in);
int a, b, result;
System.out.println("Enter first number:");
a = sc.nextInt();
System.out.println("Enter second number:");
b = sc.nextInt();
result = a / b;
System.out.println("Result of " + a + "/" + b + " is " + result);
Copy link

Choose a reason for hiding this comment

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

Please use String.format

}

private static void mul() {
Scanner sc = new Scanner(System.in);
int a, b, result;
System.out.println("Enter first number:");
a = sc.nextInt();
System.out.println("Enter second number:");
b = sc.nextInt();
result = a * b;
System.out.println("Result of " + a + "*" + b + " is " + result);
}

private static void sub() {
Scanner sc = new Scanner(System.in);
int a, b, result;
System.out.println("Enter first number:");
a = sc.nextInt();
System.out.println("Enter second number:");
b = sc.nextInt();
result = a - b;
System.out.println("Result of " + a + "-" + b + " is " + result);
}

private static void add() {
Scanner sc = new Scanner(System.in);
int a, b;
System.out.println("Enter first number:");
a = sc.nextInt();
System.out.println("Enter second number:");
b = sc.nextInt();
System.out.println("Result of " + a + "+" + b + " is " + a + b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

public class HammingDistance {
public static int compute(String a, String a1) {
int la = a.length();
int la1 = a1.length();
int count = 0;

if (la == la1) {
for (int i = 0; i < la; i++) {
if (a.charAt(i) != a1.charAt(i))
count++;
}
return count;
}
return -1;
}


}