I’ve 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 is obviously things wrong with it, i was just wondering ways in which to improve it. I think I’ve added unnecessary things. I’m practicing to use classes and other stuff.
the testing.java files contains:
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));
}
}
}
and my funcs.java file contains:
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.
Thanks
Class names should be Capitalized. EG:
ThisIsMyClassName. Field names should be camelCased. EG:thisIsAFieldName. In java, you don’t use underscores for field names. You should put the opening curly braces on the same line as the statement. EG:if ... {You can read more about these details in the java style guide:http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html