patternjavaMinor
Beginner's exercise to ask for first names and surnames
Viewed 0 times
surnamesaskexercisenamesbeginnerfirstforand
Problem
I just started Java programming yesterday (so don't expect too much), and I've written some code. My code works the way I want it to work, but there are obviously things wrong with it.
I was wondering how it could be improved. I think I've added unnecessary things. I'm practicing using classes and other stuff.
testing.java
funcs.java
(I think most of the problems can be found in the funcs.java file.)
I was wondering how it could be improved. I think I've added unnecessary things. I'm practicing using classes and other stuff.
testing.java
import java.util.Scanner;
class testing {
private static Scanner input_sn;
private static Scanner input_fn;
private static Scanner input_mem;
public static void main(String[] args){
String First_Name;
String Second_Name;
int members;
int count;
System.out.println("Members: ");
input_mem = new Scanner(System.in);
members = input_mem.nextInt();
funcs funcsObj = new funcs();
for (count = 0; count < members; count++)
{
System.out.println("What is the first name? ");
input_fn = new Scanner(System.in);
First_Name = input_fn.nextLine();
System.out.println("What is the second name? ");
input_sn = new Scanner(System.in);
Second_Name = input_sn.nextLine();
funcsObj.names( funcsObj.setFn(First_Name), funcsObj.setSn(Second_Name));
}
}
}funcs.java
public class funcs
{
private String firstName;
private String secondName;
private static int members = 0;
public String setFn(String fn)
{
firstName = fn;
return fn;
}
public String setSn(String sn)
{
secondName = sn;
return sn;
}
public void names(String fn, String sn)
{
firstName = fn;
secondName = sn;
members++;
System.out.printf("%d\t%s\t%s\n", members, fn, sn);
}
}(I think most of the problems can be found in the funcs.java file.)
Solution
Styling:
You have to fix the case of your identifiers to be more idiomatic. It means:
Also:
Code quality:
You have to fix the case of your identifiers to be more idiomatic. It means:
- Class names should be Capitalized ("Testing", "Funcs", ...)
- Class members and variables should use camel case ("firstName", "inputSn", ...)
Also:
- The method
namesof classfuncsbehaves like a "setter", so it should have a name likesetNames. In general, try to use action verbs in your method names.
- Edit: the
setXX()methods offuncsshould not return anything, as they are "setters", they are there to "set".
Code quality:
- Variables like "String First_Name" should not all be declared at the beginning of the method, but instead next to the place where they are first used. This way you limit their scope (which makes programming errors more clear) and the code is easier to read.
- Similarily, if you don't have a compelling reason to have all the Scanner instances as class members, declare them in your main method where they are used.
- Why do you use different Scanner objects for every input? You can simply use one Scanner and use its
nextLine()method 3 times. That's the only "performance" tips that you can get, I think (not that you would have any performance problem with such a short program anyway).
- Edit: In the line
funcsObj.names( funcsObj.setFn(First_Name), funcsObj.setSn(Second_Name));you are actually setting the members of yourfuncsobjects twice. First in thesetFnandsetSnmethods and then in thenamesmethod. You don't need to. Just call yoursetXXmethods. If you want to have themembersthing incremented and the data displayed, you can renamenamesintoincrementMembersand remove all setting code from there.
Context
StackExchange Code Review Q#21350, answer score: 3
Revisions (0)
No revisions yet.