Java function (method) exercises:
A: Ask for the sum of two data
B: Determine if two data is equal
C: Get the larger value in two numbers
D: Print star rectangle for m row n columns
E: print nn multiplication table
A: The procedure is as follows:
Package function;
Import Java.util.Scanner;
public class Sum {
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Data 1:");
int X=sc.nextint ();
SYSTEM.OUT.PRINTLN ("Data 2:");
int Y=sc.nextint ();
System.out.println (sum (x, y));
Sum (x, y);
}
public static int sum (int a,int b) {
int sum=a+b;
System.out.println ("Sum of two numbers:" +sum);
return sum;
}
}
B: The procedure is as follows:
Package function;
Import Java.util.Scanner;
public class Boolean {
public static void Main (string[] args) {
Scanner sc= New Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Data 1:");
int X=sc.nextint ();
SYSTEM.OUT.PRINTLN ("Data 2:");
int Y=sc.nextint ();
Boolean Flag=xiangdeng (x, y);
SYSTEM.OUT.PRINTLN ("Input two data is equal:" +flag);
}
public static Boolean Xiangdeng (int a,int b) {
Return (a==b)? True:false;
}
}
C: The program is as follows:
Package function;
Import Java.util.Scanner;
public class Max {
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter data one:");
int X=sc.nextint ();
System.out.println ("Please enter data one:");
int Y=sc.nextint ();
SYSTEM.OUT.PRINTLN ("Maximum value:");
SYSTEM.OUT.PRINTLN (max (x, y));
}
public static int max (int a,int b) {
int c= (a>b)? a:b;
return C;
}
}
D: The procedure is as follows:
Package function;
Import Java.util.Scanner;
public class Xing {
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter the number of lines:");
int X=sc.nextint ();
System.out.println ("Please enter the number of columns:");
int Y=sc.nextint ();
SYSTEM.OUT.PRINTLN ("Output result:");
Xing (x, y);
}
public static void Xing (int a,int b) {
for (int i=0;i<=a;i++) {
for (int j=0;j<=b;j++) {
System.out.print ("*");
}
System.out.println ();
}
}
}
E: The program is as follows:
Package function;
Import Java.util.Scanner;
public class Chenfa {
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter the number of lines:");
int X=sc.nextint ();
CHENFA (x);
}
public static void Chenfa (int a) {
for (int i=1;i<=a;i++) {
for (int j=1;j<=i;j++) {
System.out.print (j+ "" +i+ "=" +ij+ ' \ t ');
}
System.out.println ();
}
}
}
Java functions (methods) Exercise