I am just a beginner to programming, and Java. As I have very, very little experience with Java, I would like a simple, yet in depth explanation of this code. This code uses two classes and many methods.
Class Apples:
package jacob;
import java.util.Scanner;
class apples {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
grapes gobject = new grapes();
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine();
gobject.setName(temp);
gobject.saying();
}
}
Class Grapes:
package jacob;
public class grapes {
private String girlName;
public void setName(String name){
girlName=name;
}
public String getName(){
return girlName;
}
public void saying(){
System.out.printf("Your first gf was %s", getName());
}
}
I get confused when I get to these two parts:
gobject.setName(temp);
gobject.saying();
AND
private String girlName;
public void setName(String name){
girlName=name;
}
public String getName(){
return girlName;
}
public void saying(){
System.out.printf("Your first gf was %s", getName());
Why would one go through so much trouble when they could have done:
package jacob;
import java.util.Scanner;
public class test {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter name of first gf here: ");
String gfname = input.nextLine();
System.out.println("The name of your first gf is " + gfname + ".");
}
}
Please note that the original code is from TheNewBoston and is not mine. The second bit of code is what I have created.