1.Step 1: define the method name and return value type.
Step two: compile the method body.
2.Two cases
returns the type of the return value.
Give an example:
public class AutoLion {
//public String color;
//void
public void run(){
System.out.println(“lion running…”);
}
////return termination procedure
public String bark(){
String voice=” lion roar.“;
return voice;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
// al.run();
// // : String voice=al.bark();
System.out.println(voice); ” will deal with the result.
}
}
3.Give an example
public class AutoLion {
// > attributefilepublic String color;
//public String getColor(){
return color;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
// al.color=”red”;
String color=al.getColor();
System.out.println(color);
}
}
Nesting of methods (called)
public class AutoLion {
//public String color;
//public String getColor(){
return color;
}
////public String info(){
return ““+getColor()+”“;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
al.color=”red”;
String color=al.info();
System.out.println(color);
}
}
5.1.Student“a( )Student“b( )public void a( ) {
b( ); //b( )
}
2.Student“a( )Teacher“b( ).” call “
public void a( ) {
Teacher t = new Teacher( );
t.b( ); //Teacher“b()
}
Example: the student method calls the teacher method.



6.“
return Method can not return multiple values!
Multiple methods can not be nested each other.
You cannot write program logic code directly outside the method.
Exercise: writing batteries.
>: brand attributebrand” can be renewed.getPrower()
public class Cell {
public String brand;
public void getPrower(){
System.out.println(“…”);
}
}
public class CellTest {
public static void main(String[] args) {
Cell nanfu=new Cell();
nanfu.brand=”“;
String brand=nanfu.brand;
System.out.println(brand);
nanfu.getPrower();
}
}
Exercise: calculate average score and total score.
public class ScoreCalc {
public double java;
public double python;
public double go;
//public double getSum(){
double sum=java+python+go;
return sum;
}
//public double avg(){
double avg=getSum()/3;
return avg;
}
}
import java.util.Scanner;
public class ScoreCalcTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
ScoreCalc sc=new ScoreCalc();
System.out.println(“JAVA““);
sc.java=input.nextDouble();
System.out.println(“python““);
sc.python=input.nextDouble();
System.out.println(“go““);
sc.go=input.nextDouble();
//double sum=sc.getSum();
double avg=sc.avg();
System.out.println(““+sum+”“+avg);
}
}
7.The location of a variable declaration determines the scope of a variable.
Variable scope determines that the region of the variable can be accessed in the program according to the variable name.
8.Different scopes
1-The scope of local variables is limited to the way to define it.
2-The scope of member variables is visible inside the entire class.
Different initial values
1-Java will give an initial value to the member variable.
2-Java does not give initial values to local variables.
9. > is limited tocalcAvg()
public class Test {
int score1 = 88;
int score2 = 98;
public void calcAvg() {
int avg = (score1 + score2)/2;
}
public void showAvg(){
System.out.println(“” + avg);
}
}
Practice:
You can download music, play these music, and you can charge.
public class Phone {
//public String download(){
String music=”“;
return music;
}
//public void play(){
String music=download();
System.out.println(““+music);
}
//public void charge(){
Cell cell=new Cell();
cell.getPrower();
}
}
public class PhoneTest {
public static void main(String[] args) {
Phone a=new Phone();
a.download();
a.play();
a.charge();
}
}
Experience: nesting of this method with different methods
Exercise: achieving cascading effects of menus
import java.util.Scanner;
public class Menu {
//public void showLoginMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue){
System.out.println(““);
System.out.println(“1.“);
System.out.println(“2.“);
System.out.println(“….”);
int choice=input.nextInt() ;
if(choice==1){
showMainMenu();
break;
}else if(choice==2){
break;
}else{
System.out.println(““);
}
}
}
//public void showMainMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“————————–“);
System.out.println(““);
System.out.println(“1.2.“);
System.out.println(“….”);
int choice=input.nextInt();
if (choice==1) {
break;
}else if(choice==2){
showSendGMenu();
break;
}else if(choice==0){
showLoginMenu();
}else{
System.out.println(““);
}
}
}
// public void showSendGMenu(){
Scanner input=new Scanner(System.in);
System.out.println(“>>“);
System.out.println(“——————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
Boolean isTrue=true;
while(isTrue){
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
break;
case 2:
System.out.println(““);
break;
case 3:
System.out.println(““);
break;
default:
System.out.println(““);
break;
}
}
}
}
Test class
public class MenuTest {
public static void main(String[] args) {
Menu m=new Menu();
m.showLoginMenu();
}
}
Exercise: implementation of system entry procedures
Requirement description
,
The user name and password are entered.
Qualified entry system
package demo;
// public class Manager {
public String username=”afu”;
public String password=”123″;
public void show() {
System.out.println(“ name:”+username+” password:”+password);
}
}
package demo;
import java.util.Scanner;
public class Menu {
//public void showLoginMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue) {
System.out.println(““);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(““);
int choice=input.nextInt();
if(choice==1) {
//
break;
}else if(choice==2) {
break;
}else {
System.out.println(““);
}
}
}
//public void showMainMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
int choice=input.nextInt();
if(choice==1) {
break;
}else if(choice==2) {
showCustomerMenu();
break;
}else {
}
}
}
//public void showCustomerMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(“>>“);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
isTrue=false;
break;
case 2:
System.out.println(““);
isTrue=false;
break;
case 3:
System.out.println(““);
isTrue=false;
break;
default:
System.out.println(““);
break;
}
}
}
}
package demo;
import java.util.Scanner;
public class StartSMS {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
Manager manager=new Manager();
Menu menu=new Menu();
menu.showLoginMenu();
System.out.println(““);
String username=input.next();
System.out.println(““);
String pwd=input.next();
if(username.equals(manager.username)&&pwd.equals(manager.password)) {
System.out.println(““);
System.out.println(““+manager.username+”login”);
/* menu.showMainMenu();*/
break;
}else {
System.out.println(““);
}
}
}
}
returns the type of the return value.
Give an example:
public class AutoLion {
//public String color;
//void
public void run(){
System.out.println(“lion running…”);
}
////return termination procedure
public String bark(){
String voice=” lion roar.“;
return voice;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
// al.run();
// // : String voice=al.bark();
System.out.println(voice); ” will deal with the result.
}
}
3.Give an example
public class AutoLion {
// > attributefilepublic String color;
//public String getColor(){
return color;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
// al.color=”red”;
String color=al.getColor();
System.out.println(color);
}
}
Nesting of methods (called)
public class AutoLion {
//public String color;
//public String getColor(){
return color;
}
////public String info(){
return ““+getColor()+”“;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
al.color=”red”;
String color=al.info();
System.out.println(color);
}
}
5.1.Student“a( )Student“b( )public void a( ) {
b( ); //b( )
}
2.Student“a( )Teacher“b( ).” call “
public void a( ) {
Teacher t = new Teacher( );
t.b( ); //Teacher“b()
}
Example: the student method calls the teacher method.



6.“
return Method can not return multiple values!
Multiple methods can not be nested each other.
You cannot write program logic code directly outside the method.
Exercise: writing batteries.
>: brand attributebrand” can be renewed.getPrower()
public class Cell {
public String brand;
public void getPrower(){
System.out.println(“…”);
}
}
public class CellTest {
public static void main(String[] args) {
Cell nanfu=new Cell();
nanfu.brand=”“;
String brand=nanfu.brand;
System.out.println(brand);
nanfu.getPrower();
}
}
Exercise: calculate average score and total score.
public class ScoreCalc {
public double java;
public double python;
public double go;
//public double getSum(){
double sum=java+python+go;
return sum;
}
//public double avg(){
double avg=getSum()/3;
return avg;
}
}
import java.util.Scanner;
public class ScoreCalcTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
ScoreCalc sc=new ScoreCalc();
System.out.println(“JAVA““);
sc.java=input.nextDouble();
System.out.println(“python““);
sc.python=input.nextDouble();
System.out.println(“go““);
sc.go=input.nextDouble();
//double sum=sc.getSum();
double avg=sc.avg();
System.out.println(““+sum+”“+avg);
}
}
7.The location of a variable declaration determines the scope of a variable.
Variable scope determines that the region of the variable can be accessed in the program according to the variable name.
8.Different scopes
1-The scope of local variables is limited to the way to define it.
2-The scope of member variables is visible inside the entire class.
Different initial values
1-Java will give an initial value to the member variable.
2-Java does not give initial values to local variables.
9. > is limited tocalcAvg()
public class Test {
int score1 = 88;
int score2 = 98;
public void calcAvg() {
int avg = (score1 + score2)/2;
}
public void showAvg(){
System.out.println(“” + avg);
}
}
Practice:
You can download music, play these music, and you can charge.
public class Phone {
//public String download(){
String music=”“;
return music;
}
//public void play(){
String music=download();
System.out.println(““+music);
}
//public void charge(){
Cell cell=new Cell();
cell.getPrower();
}
}
public class PhoneTest {
public static void main(String[] args) {
Phone a=new Phone();
a.download();
a.play();
a.charge();
}
}
Experience: nesting of this method with different methods
Exercise: achieving cascading effects of menus
import java.util.Scanner;
public class Menu {
//public void showLoginMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue){
System.out.println(““);
System.out.println(“1.“);
System.out.println(“2.“);
System.out.println(“….”);
int choice=input.nextInt() ;
if(choice==1){
showMainMenu();
break;
}else if(choice==2){
break;
}else{
System.out.println(““);
}
}
}
//public void showMainMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“————————–“);
System.out.println(““);
System.out.println(“1.2.“);
System.out.println(“….”);
int choice=input.nextInt();
if (choice==1) {
break;
}else if(choice==2){
showSendGMenu();
break;
}else if(choice==0){
showLoginMenu();
}else{
System.out.println(““);
}
}
}
// public void showSendGMenu(){
Scanner input=new Scanner(System.in);
System.out.println(“>>“);
System.out.println(“——————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
Boolean isTrue=true;
while(isTrue){
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
break;
case 2:
System.out.println(““);
break;
case 3:
System.out.println(““);
break;
default:
System.out.println(““);
break;
}
}
}
}
Test class
public class MenuTest {
public static void main(String[] args) {
Menu m=new Menu();
m.showLoginMenu();
}
}
Exercise: implementation of system entry procedures
Requirement description
,
The user name and password are entered.
Qualified entry system
package demo;
// public class Manager {
public String username=”afu”;
public String password=”123″;
public void show() {
System.out.println(“ name:”+username+” password:”+password);
}
}
package demo;
import java.util.Scanner;
public class Menu {
//public void showLoginMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue) {
System.out.println(““);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(““);
int choice=input.nextInt();
if(choice==1) {
//
break;
}else if(choice==2) {
break;
}else {
System.out.println(““);
}
}
}
//public void showMainMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
int choice=input.nextInt();
if(choice==1) {
break;
}else if(choice==2) {
showCustomerMenu();
break;
}else {
}
}
}
//public void showCustomerMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(“>>“);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
isTrue=false;
break;
case 2:
System.out.println(““);
isTrue=false;
break;
case 3:
System.out.println(““);
isTrue=false;
break;
default:
System.out.println(““);
break;
}
}
}
}
package demo;
import java.util.Scanner;
public class StartSMS {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
Manager manager=new Manager();
Menu menu=new Menu();
menu.showLoginMenu();
System.out.println(““);
String username=input.next();
System.out.println(““);
String pwd=input.next();
if(username.equals(manager.username)&&pwd.equals(manager.password)) {
System.out.println(““);
System.out.println(““+manager.username+”login”);
/* menu.showMainMenu();*/
break;
}else {
System.out.println(““);
}
}
}
}
b( ); //b( )
}
2.Student“a( )Teacher“b( ).” call “
public void a( ) {
Teacher t = new Teacher( );
t.b( ); //Teacher“b()
}
Example: the student method calls the teacher method.
6.“
return Method can not return multiple values!
Multiple methods can not be nested each other.
You cannot write program logic code directly outside the method.
Exercise: writing batteries.
>: brand attributebrand” can be renewed.getPrower()
public class Cell {
public String brand;
public void getPrower(){
System.out.println(“…”);
}
}
public class CellTest {
public static void main(String[] args) {
Cell nanfu=new Cell();
nanfu.brand=”“;
String brand=nanfu.brand;
System.out.println(brand);
nanfu.getPrower();
}
}
Exercise: calculate average score and total score.
public class ScoreCalc {
public double java;
public double python;
public double go;
//public double getSum(){
double sum=java+python+go;
return sum;
}
//public double avg(){
double avg=getSum()/3;
return avg;
}
}
import java.util.Scanner;
public class ScoreCalcTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
ScoreCalc sc=new ScoreCalc();
System.out.println(“JAVA““);
sc.java=input.nextDouble();
System.out.println(“python““);
sc.python=input.nextDouble();
System.out.println(“go““);
sc.go=input.nextDouble();
//double sum=sc.getSum();
double avg=sc.avg();
System.out.println(““+sum+”“+avg);
}
}
7.The location of a variable declaration determines the scope of a variable.
Variable scope determines that the region of the variable can be accessed in the program according to the variable name.
8.Different scopes
1-The scope of local variables is limited to the way to define it.
2-The scope of member variables is visible inside the entire class.
Different initial values
1-Java will give an initial value to the member variable.
2-Java does not give initial values to local variables.
9. > is limited tocalcAvg()
public class Test {
int score1 = 88;
int score2 = 98;
public void calcAvg() {
int avg = (score1 + score2)/2;
}
public void showAvg(){
System.out.println(“” + avg);
}
}
Practice:
You can download music, play these music, and you can charge.
public class Phone {
//public String download(){
String music=”“;
return music;
}
//public void play(){
String music=download();
System.out.println(““+music);
}
//public void charge(){
Cell cell=new Cell();
cell.getPrower();
}
}
public class PhoneTest {
public static void main(String[] args) {
Phone a=new Phone();
a.download();
a.play();
a.charge();
}
}
Experience: nesting of this method with different methods
Exercise: achieving cascading effects of menus
import java.util.Scanner;
public class Menu {
//public void showLoginMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue){
System.out.println(““);
System.out.println(“1.“);
System.out.println(“2.“);
System.out.println(“….”);
int choice=input.nextInt() ;
if(choice==1){
showMainMenu();
break;
}else if(choice==2){
break;
}else{
System.out.println(““);
}
}
}
//public void showMainMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“————————–“);
System.out.println(““);
System.out.println(“1.2.“);
System.out.println(“….”);
int choice=input.nextInt();
if (choice==1) {
break;
}else if(choice==2){
showSendGMenu();
break;
}else if(choice==0){
showLoginMenu();
}else{
System.out.println(““);
}
}
}
// public void showSendGMenu(){
Scanner input=new Scanner(System.in);
System.out.println(“>>“);
System.out.println(“——————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
Boolean isTrue=true;
while(isTrue){
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
break;
case 2:
System.out.println(““);
break;
case 3:
System.out.println(““);
break;
default:
System.out.println(““);
break;
}
}
}
}
Test class
public class MenuTest {
public static void main(String[] args) {
Menu m=new Menu();
m.showLoginMenu();
}
}
Exercise: implementation of system entry procedures
Requirement description
,
The user name and password are entered.
Qualified entry system
package demo;
// public class Manager {
public String username=”afu”;
public String password=”123″;
public void show() {
System.out.println(“ name:”+username+” password:”+password);
}
}
package demo;
import java.util.Scanner;
public class Menu {
//public void showLoginMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue) {
System.out.println(““);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(““);
int choice=input.nextInt();
if(choice==1) {
//
break;
}else if(choice==2) {
break;
}else {
System.out.println(““);
}
}
}
//public void showMainMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(““);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
int choice=input.nextInt();
if(choice==1) {
break;
}else if(choice==2) {
showCustomerMenu();
break;
}else {
}
}
}
//public void showCustomerMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println(“>>“);
System.out.println(“—————————————————“);
System.out.println(“1-“);
System.out.println(“2-“);
System.out.println(“3-“);
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println(““);
isTrue=false;
break;
case 2:
System.out.println(““);
isTrue=false;
break;
case 3:
System.out.println(““);
isTrue=false;
break;
default:
System.out.println(““);
break;
}
}
}
}
package demo;
import java.util.Scanner;
public class StartSMS {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
Manager manager=new Manager();
Menu menu=new Menu();
menu.showLoginMenu();
System.out.println(““);
String username=input.next();
System.out.println(““);
String pwd=input.next();
if(username.equals(manager.username)&&pwd.equals(manager.password)) {
System.out.println(““);
System.out.println(““+manager.username+”login”);
/* menu.showMainMenu();*/
break;
}else {
System.out.println(““);
}
}
}
}
1-The scope of local variables is limited to the way to define it.
2-The scope of member variables is visible inside the entire class.
Different initial values
1-Java will give an initial value to the member variable.
2-Java does not give initial values to local variables.