java - Simple yet in depth explanation of this code -


i beginner programming, , java. have very, little experience java, simple, yet in depth explanation of code. code uses 2 classes , 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 %s", getname()); } } 

i confused when these 2 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 %s", getname()); 

why 1 go through trouble when 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 first gf " + gfname + "."); } } 

please note original code thenewboston , not mine. second bit of code have created.

your question fundamentals of object oriented programming. reason confusion method saying() , whole program trivial. in non trivial program impossible avoid using method or object. since body of method 1 line can replace simple println if body more complex have hard time repeating more 1 name. example let's assume method saying() this:

public void saying(){     string name = getname(); //saves name in variable     stringbuilder reverse = new stringbuilder(name.length()); //creates new variable hold reversed name     for(i=name.length()-1;i>=0;i--){ //this loops through characters in name , makes reversed name         reverseapend(name.charat(i);     }     system.out.println("the reverse of gf name is" + reverse + "."); } 

this method still prints 1 line more complex body. consider want reverse 3 names , print them. 1 easier setting field , calling method 3 times or typing whole method body 3 times? if want reverse string in part o program. can reuse method instead of typing chunk of code every time need it. 1 of many benefits of using objects. can read abstraction, encapsulation , inheritance understand importance of objects. broad topic accommodated in simple answer. best thing read book object oriented programming.


Comments

Popular posts from this blog

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -