patternjavaMinor
Age Distance Calculator
Viewed 0 times
agedistancecalculator
Problem
Here is my final build of the Age Distance Calculator - Any feedback on the code would be great - This is my 2nd day of java programming and thought this was a good little project.
```
package dev.swift.atc;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class AgeCalc {
private int personAge;
private int age;
private int d_age;
private String name;
static Scanner s = new Scanner(System.in);
public static void main(String [] args) throws InterruptedException
{
AgeCalc ac = new AgeCalc();
ac.getInputs();
boolean result = ac.calculateAge();
if (result == true) {
ac.showResults();
s.close();
}
else {
ac.errorResults();
TimeUnit.SECONDS.sleep(3);
System.out.println("Restarting Program");
TimeUnit.SECONDS.sleep(2);
ac.ageCalculateTwo();
}
}
//The constructor
public AgeCalc() throws InterruptedException {
System.out.println("Welcome To Java Coded Age Differnce Calculator");
TimeUnit.MILLISECONDS.sleep(100);
}
//Handle inputs
public void getInputs() throws InterruptedException {
System.out.println("Input Your Name Before We Start");
this.name = s.nextLine();
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Input Your Age");
this.age = s.nextInt();
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Your Age Is " + this.age + " Years Old.");
}
//Make the calculation
public boolean calculateAge() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("Now Input The Desired Age To Work Out The Distance To");
this.d_age = s.nextInt();
if (this.d_age < this.age){
System.out.println("You Cannot Set The Target To Lower Then Your Current Age");
return false;
}
else {
this.personAge = this.d_age - this.age;
```
package dev.swift.atc;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class AgeCalc {
private int personAge;
private int age;
private int d_age;
private String name;
static Scanner s = new Scanner(System.in);
public static void main(String [] args) throws InterruptedException
{
AgeCalc ac = new AgeCalc();
ac.getInputs();
boolean result = ac.calculateAge();
if (result == true) {
ac.showResults();
s.close();
}
else {
ac.errorResults();
TimeUnit.SECONDS.sleep(3);
System.out.println("Restarting Program");
TimeUnit.SECONDS.sleep(2);
ac.ageCalculateTwo();
}
}
//The constructor
public AgeCalc() throws InterruptedException {
System.out.println("Welcome To Java Coded Age Differnce Calculator");
TimeUnit.MILLISECONDS.sleep(100);
}
//Handle inputs
public void getInputs() throws InterruptedException {
System.out.println("Input Your Name Before We Start");
this.name = s.nextLine();
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Input Your Age");
this.age = s.nextInt();
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Your Age Is " + this.age + " Years Old.");
}
//Make the calculation
public boolean calculateAge() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("Now Input The Desired Age To Work Out The Distance To");
this.d_age = s.nextInt();
if (this.d_age < this.age){
System.out.println("You Cannot Set The Target To Lower Then Your Current Age");
return false;
}
else {
this.personAge = this.d_age - this.age;
Solution
I would use more legible names for variables and classes, they make it easier to read code as plain text (which is useful for other people to understand your code).
For example: naming your class AgeCalculator instead of AgeCalc, or ageCalculator instead of ac when instantiating it.
It is not that important on this example because it's not an extensive code, but if it where, it would be very difficult even for yourself (as the code owner) to remember what all variables mean, for further explanation I would suggest reading the disadvantages paragraph of https://en.wikipedia.org/wiki/Hungarian_notation
Another thing I could suggest you is to have your main method outside AgeCalculator, you should only keep inside methods that represent its behaviour.
I would start practising those, good luck!
EDIT
I found an example at http://www.tutorialspoint.com/java/java_object_classes.htm
I modified it a bit to also apply what I understand are more legible names:
As you can see, variable names get a bit longer, but they also state what they are for, just by reading their names, so you don't have to remember "What did 'asdf' mean??" xD get me?
Also take into account that in complex code, you probably would have to be remembering many variables, and sometimes it won't be your code at all! So you won't be familiar with it.
Now, the main method, should be in another class (probably another file):
Because as I said, you should only keep inside a class, methods that represent it's behaviour. At least in Object Oriented Programming, like this case, because each time you initialise a new Object (new Employee), you want it to behave like an Employee and expect it to have Employee methods, like calculating it's salary for example, or getting it's name. But a method called "main" doesn't make sense at all, an employee could give you his salary or name, not his... main?
Also remember that main is a special method that represents the beginning of the execution of your code, it should be separated from the rest of your objects and it must be unique.
Well hope I've helped some, I've also much to learn myself, good luck!
For example: naming your class AgeCalculator instead of AgeCalc, or ageCalculator instead of ac when instantiating it.
It is not that important on this example because it's not an extensive code, but if it where, it would be very difficult even for yourself (as the code owner) to remember what all variables mean, for further explanation I would suggest reading the disadvantages paragraph of https://en.wikipedia.org/wiki/Hungarian_notation
Another thing I could suggest you is to have your main method outside AgeCalculator, you should only keep inside methods that represent its behaviour.
I would start practising those, good luck!
EDIT
I found an example at http://www.tutorialspoint.com/java/java_object_classes.htm
I modified it a bit to also apply what I understand are more legible names:
import java.io.*;
public class Employee{
String name;
int age;
String designation;
double salary;
// This is the constructor of the class Employee
public Employee(String name){
this.name = name;
}
// Assign the age of the Employee to the variable age.
public void setAge(int newAge){
age = newAge;
}
/* Assign the designation to the variable designation.*/
public void setDesignation(String newDesignation){
designation = newDesignation;
}
/* Assign the salary to the variable salary.*/
public void setSalary(double newSalary){
salary = newSalary;
}
/* Print the Employee details */
public void printEmployee(){
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}As you can see, variable names get a bit longer, but they also state what they are for, just by reading their names, so you don't have to remember "What did 'asdf' mean??" xD get me?
Also take into account that in complex code, you probably would have to be remembering many variables, and sometimes it won't be your code at all! So you won't be familiar with it.
Now, the main method, should be in another class (probably another file):
import java.io.*;
public class TheClassThatHasMainMethod{
public static void main(String args[]){
/* Create two objects using constructor */
Employee firstEmployee = new Employee("James Smith");
Employee secondEmployee = new Employee("Mary Anne");
// Invoking methods for each object created
firstEmployee.setAge(26);
firstEmployee.setDesignation("Senior Software Engineer");
firstEmployee.setSalary(1000);
firstEmployee.printEmployee();
secondEmployee.setAge(21);
secondEmployee.setDesignation("Software Engineer");
secondEmployee.setSalary(500);
secondEmployee.printEmployee();
}
}Because as I said, you should only keep inside a class, methods that represent it's behaviour. At least in Object Oriented Programming, like this case, because each time you initialise a new Object (new Employee), you want it to behave like an Employee and expect it to have Employee methods, like calculating it's salary for example, or getting it's name. But a method called "main" doesn't make sense at all, an employee could give you his salary or name, not his... main?
Also remember that main is a special method that represents the beginning of the execution of your code, it should be separated from the rest of your objects and it must be unique.
Well hope I've helped some, I've also much to learn myself, good luck!
Code Snippets
import java.io.*;
public class Employee{
String name;
int age;
String designation;
double salary;
// This is the constructor of the class Employee
public Employee(String name){
this.name = name;
}
// Assign the age of the Employee to the variable age.
public void setAge(int newAge){
age = newAge;
}
/* Assign the designation to the variable designation.*/
public void setDesignation(String newDesignation){
designation = newDesignation;
}
/* Assign the salary to the variable salary.*/
public void setSalary(double newSalary){
salary = newSalary;
}
/* Print the Employee details */
public void printEmployee(){
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}import java.io.*;
public class TheClassThatHasMainMethod{
public static void main(String args[]){
/* Create two objects using constructor */
Employee firstEmployee = new Employee("James Smith");
Employee secondEmployee = new Employee("Mary Anne");
// Invoking methods for each object created
firstEmployee.setAge(26);
firstEmployee.setDesignation("Senior Software Engineer");
firstEmployee.setSalary(1000);
firstEmployee.printEmployee();
secondEmployee.setAge(21);
secondEmployee.setDesignation("Software Engineer");
secondEmployee.setSalary(500);
secondEmployee.printEmployee();
}
}Context
StackExchange Code Review Q#138376, answer score: 3
Revisions (0)
No revisions yet.