Many novices, beginners and freshmen are in contact with programming for the first time. They don't know how to improve their programming ability and how to get started programming quickly. Remember all kinds of code. In fact, there is only one way to go to the big man!! Crazy code!!!
1. Simulate system function overload, compatible data type and sum.
The code is as follows (example):
public class MethoDemo4 { public static void main(String[] args) { int x = sum(4,5); } //Method overloading public static int sum(int a,int b){ return a + b; } public static double sum(double a,double b){ return a + b; } public static int sum(int a,int b ,int c){ return a + b + c ; } }
2. Change the value in the array by calling the method
The code is as follows (example):
public class MethodDemo6 { public static void main(String[] args) { int[] arr = {10,20,30}; System.out.println("call change Before method:" + arr[1]); change(arr); System.out.println("call change After the method:" + arr[1]); } public static void change(int[] arr){ arr[1] =200; } }
3. Define abstract methods and abstract classes
The code is as follows (example):
//Abstract methods and abstract classes public abstract class Animal { public abstract void eat(); }
4. Create the implementation class of the abstract class and implement the abstract method
The code is as follows (example):
/* abstract class */ public abstract class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //Abstract method public abstract void eat(); }
/* Implementation class */ public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } public void eat(){ System.out.println("Cats eat fish"); } }
/* The implementation class of polymorphic creation abstract class */ public class AnimalDemo { public static void main(String[] args) { Animal c = new Cat(); c.eat(); Animal d = new Dog(); d.eat(); } }
5. Create an array, find the maximum value in the array through the for loop, and output it
The code is as follows (example):
public class ArrayDemo6 { public static void main(String[] args) { int [] arr ={12,24,36,54,25}; int max= arr[0]; for(int x =1 ; x < arr.length ; x++){ if (max < arr[x]){ max = arr[x]; } } System.out.println(max); } }
6. Have a preliminary understanding of ArrayList and insert data into the list through the add method
The code is as follows (example):
/* ArrayList Structural offence: public ArrayList():Create an empty collection object ArrayList Add method: public booleam add(E e):Appends the specified element to the end of this collection public void add(int index,E element):Inserts the specified content at the specified location in this collection */ public class ArrayListDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<String>(); System.out.println("Array:" + array); array.add("I love java"); System.out.println(array); array.add(1,"java"); System.out.println(array); } }
Package involved: ArrayList
7. Be familiar with various methods in ArrayList.
public boolean remove(Object to) deletes the specified element and returns whether it is successful public E remove (int index) deletes the specified element and returns whether it is successful Public e set (int index, e element) modifies the index element and returns the modified element public E get(int index) returns the index element public int size() returns the number of elements in the collection
The code is as follows (example):
public class ArrayListDemo_01 { public static void main(String[] args) { ArrayList array = new ArrayList(); array.add("programing language"); array.add("java"); array.add("python"); System.out.println("array:"+ array); array.remove("C"); System.out.println("array:"+array); array.add(0,"C++"); System.out.println("--------------------"); System.out.println("array:"+array); array.remove(2); System.out.println("array:"+array); array.add(2,"Java"); System.out.println("---------------------"); System.out.println("array:"+array); array.set(0,"Lee"); System.out.println("array:"+array); array.set(0,"paragraph"); System.out.println("----------------------"); Object s = array.get(0); System.out.println(s); System.out.println("------------------------"); final int size = array.size(); System.out.println(size); } }
Package involved: ArrayList
8. Create a Student class, create a Student class ArrayList, enter the Student ID and name through user input, and save it to ArrayList.
The code is as follows (example):
/* Student class */ public class Student { private String name; private int age; public Student(){} public Student(String name, int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setage(int age){ if (age <0 || age >100){ System.out.println("Input error"); }else { this.age = age; } } public int getage(){ return age; } }
import java.util.ArrayList; import java.util.Scanner; public class ArrayListTest01 { public static void main(String[] args) { Student [] arr = new Student[3]; ArrayList<Student> array = new ArrayList<Student>(); for (int i = 0; i< arr.length;i++) { Scanner sc = new Scanner(System.in); System.out.println("Please enter student name:"); String stu1 = sc.nextLine(); System.out.println("Please enter student age:"); int stu2 = sc.nextInt(); Student s1 = new Student(stu1, stu2); arr[i]=s1; array.add(arr[i]); } for (int i =0; i<array.size() ;i++){ Student s = array.get(i); System.out.println(s.getName()+","+s.getage()); } } }
Packages involved: ArrayList, Scanner
9. Create a set to store student objects, store three student objects, and use the console to traverse the set
Idea: 1. Define student category 2. Create a collection object 3. Create student objects 4. Add student objects to the collection 5. Traverse the set, which is implemented in general traversal format
The code is as follows (example):
public class ArrayListTest02 { public static void main(String[] args) { //Create collection object ArrayList<Student> array = new ArrayList<Student>(); //Create student object Student s1 = new Student("Duan Yale",18); Student s2 = new Student("Breezy",19); Student s3 = new Student ("Lin Qingxia", 20); //Add student object to collection array.add(s1); array.add(s2); array.add(s3); for (int i = 0; i<array.size();i++){ Student s = array.get(i); System.out.println(s.getName()+","+s.getage()); } } }
Package involved: ArrayList
10. Self built method. By calling the method, the user can input the student data and save it in the ArrayList, traverse the ArrayList and output all student objects
The code is as follows (example):
import java.util.ArrayList; import java.util.Scanner; public class ArrayListTest03 { public static void main(String[] args) { ArrayList<Student> array = new ArrayList<Student>(); addStudent(array); addStudent(array); addStudent(array); for (int i = 0 ; i<array.size(); i++){ Student s = array.get(i); System.out.println(s.getName() + "," +s.getage() ); } } public static void addStudent(ArrayList<Student> array ){ Scanner sc = new Scanner(System.in); System.out.println("Please enter your name:"); String s = sc.nextLine(); System.out.println("Please enter age:"); int s1 = sc.nextInt(); Student stu = new Student(); stu.setName(s); stu.setage(s1); array.add(stu); } }
summary
Doing some basic programming every day and gradually improving your programming ability plays a vital role in learning a language better in the future. I will continue to update some basic code every day for beginners to use. Thank you for your support~~