In java, week2 class has no parameters or parameter-based methods. javaweek2
2.1 syntax
Public return value type method name (){
// Method body
}
2.2 call Syntax of a Method
Object Name. Method Name
Calculate average score and total score
1 public class Score {2/** 3 * Create class ScoreCalc write Method Implement each function write test class 4 * receive three course scores from the keyboard (java c # db) 5 * calculate the average score and total score of the three courses. 6 * write the score calculation class implementation function _______ 7 */8 int java; 9 int c; 10 int db; 11 12 public double avg () {13 double a = (double) (java + c + db)/3; 14 return a; 15} 16 17 public int addAll () {18 19 int ad = java + c + db; 20 return ad; 21} 22 23}
Score
1 import java. util. optional; 2 3 4 public class TestScore {5 public static void main (String [] args) {6 Score SC = new Score (); 7 bytes input = new partition (System. in); 8 System. out. println ("Enter the java score:"); 9 SC. java = input. nextInt (); 10 System. out. println ("Enter the c score:"); 11 SC. c = input. nextInt (); 12 System. out. println ("Enter the db score:"); 13 SC. db = input. nextInt (); 14 15 System. out. println (SC. addAll (); // The total score is 16 System. out. println (SC. avg (); // average score 17} 18 19}
TestScore
Package week2; public class Manager {String name; String password;/*** print student information */public void show () {System. out. println ("Administrator name:" + name + "\ n password:" + password );}}
1 package week2; 2 3 public class TestManager {4 public static void main (String [] args) {5 Manager manager = new Manager (); 6 manager. name = "WANG Duo"; 7 manager. password = "123"; 8 manager. show (); 9} 10 11}
TestManager
2.3 use arrays as parameters
Package week2; public class extends core {public double avg (int [] scores) {int sum = 0; double avg1 = 0.0; for (int I = 0; I <scores. length; I ++) {sum = sum + scores [I];} avg1 = (double) sum/scores. length; return avg1;} public int max (int [] scores) {int max1 = scores [0]; for (int I = 0; I <scores. length; I ++) {if (max1 <scores [I]) {// maximum max1 = scores [I] ;}return max1 ;}}
Package week2; import java. util. secret; public class TestScore {public static void main (String [] args) {JavaScore javascore = new JavaScore (); int [] scores = new int [4]; wrote input = new partition (System. in); System. out. println ("Enter the average score of five contestants:"); for (int I = 0; I <scores. length; I ++) {scores [I] = input. nextInt ();} // pingj1_core. avg (scores );}}
Methods With over 2.4 Parameters
Package week2; public class StudentBz {String [] names = new String [30]; // Add public void addName (String name) {for (int I = 0; I <names. length; I ++) {if (names [I] = null) {names [I] = name; break ;}}// displays the student information public void nameShow () {for (int I = 0; I <names. length; I ++) {if (names [I]! = Null) {System. out. print (names [I] + ",") ;}} public boolean searchFind (int start, int end, String name) {boolean find = false; // identify the position to be searched for (int I = start-1; I <end; I ++) {// I = start-1sss array subscript if (names [I]. equals (name) {find = true; break;} return find;} public boolean updateName (String newName, String oldName) {boolean find = false; // find the old namefor (int I = 0; I <names. length; I ++) {if (oldName. equals (names [I]) {names [I] = newName; find = true; break ;}} return find ;}}
Package week2; import java. util. extends; public class TestAdd {public static void main (String [] args) {StudentBz stu = new StudentBz (); for (int I = 0; I <3; I ++) {partition input = new partition (System. in); System. out. print ("Enter the Student name:"); String na = input. next (); stu. addName (na);} stu. nameShow (); boolean result = stu. searchFind (2, 5, "name2"); System. out. println (result); boolean updateResult = stu. updateName ("newName", "name2"); System. out. println ("modified successfully" + updateResult); stu. nameShow ();}}
2.5 objects as parameters
1 package week2;2 3 public class Student {4 String name;5 int age;6 int no;7 int score;8 9 }
Student
1 package week2; 2 3 public class StudentBz2 { 4 5 Student students[] = new Student[30]; 6 7 public void addStudent(Student student) { 8 for (int i = 0; i < students.length; i++) { 9 if (students[i] == null) {10 students[i] = student;11 break;12 }13 14 }15 16 }17 18 public void showStudent() {19 for (int i = 0; i < students.length; i++) {20 if (students[i] != null) {21 System.out.println(students[i].name + "," + students[i].age22 + "," + students[i].no + "," + students[i].score + ",");23 24 }25 26 }27 28 }29 30 }
StudentBz2
1 package week2; 2 3 import java. util. required; 4 5 public class TestStudentBz2 {6 public static void main (String [] args) {7 StudentBz2 stubz = new StudentBz2 (); 8 bytes input = new bytes (System. in); 9 for (int I = 0; I <2; I ++) {10 Student stu = new Student (); // several objects appear in the loop several times 11 System. out. print ("Enter name:"); 12 stu. name = input. next (); 13 System. out. print ("Enter age:"); 14 stu. age = input. nextInt (); 15 System. out. print ("Enter your student ID:"); 16 stu. no = input. nextInt (); 17 System. out. print ("Enter the score:"); 18 stu. score = input. nextInt (); 19 stubz. addStudent (stu); 20} 21 System. out. println (); 22 23 stubz. showStudent (); 24 25} 26 27}
TestStudentBz2
Exercise to sort Customer names
1 package week2; 2 3 import java.util.Arrays; 4 5 public class KH { 6 7 public void sortName(String []names) { 8 Arrays.sort(names); 9 10 }11 }
KH
1 package week2; 2 3 public class KHTest {4 public static void main (String [] args) {5 String [] names = {"j", "sds ", "sdssf", "ssf"}; 6 KH kh = new KH (); 7 System. out. println ("Before sorting"); 8 for (int I = 0; I <names. length; I ++) {9 if (names [I]! = Null) {10 System. out. println (names [I] + ","); 11 12} 13 14} 15 kh. sortName (names); 16 System. out. println ("sorted"); 17 for (int I = 0; I <names. length; I ++) {18 if (names [I]! = Null) {19 System. out. println (names [I] + ","); 20 21 22} 23 24 25} 26} 27 28}
KHTest
Exercise with Parameters
1 package week2; 2 3 public class CustomerBiz {4 String [] customer = new String [30]; 5 6 public void addName (String name) {7 for (int I = 0; I <customer. length; I ++) {8 if (customer [I] = null) {9 customer [I] = name; 10 break; 11 12} 13 14 15} 16 17 18} 19 20 21 public void showNames () {22 System. out. println ("*******************"); 23 System. out. println ("customer Name List"); 24 25 for (int I = 0; I <customer. length ; I ++) {26 if (customer [I]! = Null) {27 System. out. println (customer [I] + ","); 28 29} 30 31 32} 33 34 35 36} 37 38}
CustomerBiz
1 package week2; 2 3 import java. util. secret; 4 5 public class TestCustomer {6 public static void main (String [] args) {7 boolean con = false; 8 CustomerBiz cust = new CustomerBiz (); 9 bytes input = new partition (System. in); 10 11 for (int I = 0; I <3; I ++) {12 System. out. print ("Enter customer name:"); 13 String na = input. next (); 14 cust. addName (na); 15 System. out. println ("continue (y/n)"); 16 String n = input. next ( ); 17 18 if (! N. equals ("y") {19 20} 21 22} 23 cust. showNames (); 24 25} 26 27}
TestCustomer