Java Foundation Sixth Day

Source: Internet
Author: User
Tags int size modifier macbook

# # #01引用数据类型_类
* A: Data type
* Data types in A:java are divided into: basic types and reference types
* B: Classification of reference types
* A:java provides us with good classes, such as: Scanner,random and so on.
* B: We create our own classes, according to the definition of the class, you can include multiple methods and properties in the class for us to use.


# # #02自定义类的概述
* A: Overview of Custom Classes
* The process of defining a class is when the Java code is mapped into a real thing.
For example
So we're going to take a cell phone and analyze it, what can it do? It can call, surf the Internet, chat and so on, these are the functions provided by the mobile phone, that is, the method, mobile phone also has its characteristics, such as color, size, brand model, etc., these are the characteristics of mobile phones, that is, attributes
* Currently, we only focus on the properties in the class, and the methods in the class are then learned in the object-oriented part.


# # #03自定义类的格式
* A: The format of the custom class
* A: Use the form of a class to describe things in the real world.
* B: Things are made up of two parts, method and property.
* Method: This thing has the function.
* attribute: The characteristic of this thing.
* C: Format
public class class Name {
Property Definition
Modifier data type variable name = value

Method definition
Modifier returns a value type method name (parameter list) {

}
}


# # #04自定义的手机类
* A: Custom phone class
* A: Case code
public class phone{
/*
Defining the properties of a phone
*/
String color;
String brand;
Double size;
}


# # #05测试手机类
* A: Call method Execution Flow
* A: Steps to implement a reference type
* 1: Import packages, classes are in the same folder, no need to import packages
* 2: Create a variable of reference type
* 3: variable. Functions in the Type
* B: Case code
public class testphone{
public static void Main (string[] args) {
2: Create a variable of reference type
Phone p = new phone ();
SYSTEM.OUT.PRINTLN (P); Address of output memory

3: variable. Functions in the Type
Variable p., calling the property in the class
Properties are variables, assignments, and get values
P.color = "Local tyrants gold";
P.brand = "Ericsson";
P.size = 5.0;

Get property value
System.out.println (p.color+ "" +p.brand+ "" +p.size ");
}
}

# # #06自定义类的内存图_1
* A: Memory diagram of the custom class _1
* A: See \day06\day06 (Object-oriented \day06_source\ object memory graph. Jpg


# # #07自定义类的内存图_2
* A: Memory diagram of the custom class _1
* A: See \day06\day06 (Object-oriented \day06_source\ object memory graph. Jpg


# # #08两个引用类型变量内存图
* A: Memory diagram of the custom class _1
* A: See \day06\day06 (Object-oriented \day06_source\ two reference variable memory graph. Jpg


# # #09自定义类的练习
* A: Code for the entity class
/*
Rice cooker with properties (brand, capacity size, color, etc.)
Define classes, describe things, rice cookers
Properties: brand, size, color

Definition class, class name, Rice cooker
Scope of the class, defining three properties
*/
public class dianfanguo{
Define three properties
String brand;
Double size;
String color;
}

/*
Automotive, including attributes (brand, displacement, type, etc.)
Define class, class name Car
Property brand Displacement Type
*/
public class car{
Defining Cars three Properties
String brand;
Double Pailiang;
String type;
}

/*
Students, including attributes (name, age, gender, etc.)
Define class, class name student
Three attributes: Name, age, Gender (char)
*/
public class student{
String name;
int age;
char sex;
}

* B: Test class Code
/*
Defined test class
simultaneous test, rice cooker, car, student
*/
public class test{
public static void Main (string[] args) {
//Create Rice Cooker Reference type
Dianfanguo DFG = new Dianfanguo ();

Dfg.brand = "Tesla";
Dfg.color = "Red";
Dfg.size = 30;

System.out.println (dfg.brand+ "" +dfg.color+ "" +dfg.size);

//Create car reference type
Car c = new car ();
C.brand = "giant Force";
C.type = "Tractor";
C.pailiang = 0.5;

System.out.println (c.brand+ "" +c.type+ "" +c.pailiang);

//Create student reference type
Student stu = new Student ();
Stu.name = "Zhang San";
Stu.age = 20;
Stu.sex = ' male ';
System.out.println (stu.name+ "" +stu.age+ "" +stu.sex ");

}
}



# # #10ArrayList创建变量的步骤
* a:arraylist to create a variable
* A: Import Package Java.util Package
* B: Create a variable of reference type
data type < Collection stored data type > variable name = new data type < collection stored data type > ();
Collection stored data type: To store data in a container in a collection
to create a collection reference variable, you must specify what type of storage is
* C: Variable name. Method
Note: Collection of stored data, 8 base types corresponding to 8 reference types
Store reference types , does not store the base type

# # #11ArrayList创建变量举例
* A:arraylist example code for creating variables
Import java.util.ArrayList;
public class arraylistdemo{
public static void Main (string[] args) {
//Create collection container, specify stored data type
//storage string
ArrayList <String> array = new arraylist<string> ();

//Create collection container, store integer
arraylist<integer> array2 = new arraylist<integer> ();

//Create collection container, store phone type
arraylist<phone> array3 = new arraylist<phone> ();
}
}


# # # #12ArrayList的常见方法
* A:arraylist Common methods
* A:ADD (parameters) add elements to the collection
* b:get (int index) take the elements in the collection, the parameters of the Get method, Write Index
* c:size () returns the length of the collection, the number of elements stored in the collection
* B: Case code
Import java.util.ArrayList;
public class arraylistdemo_1{
public static void Main (string[] args) {
//definition collection, storing string elements
Arraylist<string > array = new arraylist<string> ();
//Call collection method Add storage element
Array.add ("abc");
Array.add ("Itcast");
Array.add ("Love");
Array.add ("Java"); The length of the
//output collection, calling the collection method size, the return value type of the size method int
int size = Array.size ();
SYSTEM.OUT.PRINTLN (size);

//Gets an element from the collection, gets the 1 indexed element
//collection method get, gets the element after the result data type
String s = array.get (1);
System.out.println (s);


System.out.println (array.get (0));
System.out.println (Array.get (1));
System.out.println (Array.get (2));
System.out.println (Array.get (3));
}
}


# # #13ArrayList集合的遍历
* A: Case code
/*
Collection Traversal
Implementation thought also index thought
Collection index starting at 0, to size ()-1
Method get (int index)
*/
Import java.util.ArrayList;
public class arraylistdemo_2{
public static void Main (string[] args) {
Arraylist<integer> array = new Arraylist<integer> ();
Array.add (121);
Array.add (125);
Array.add (123);
Array.add (120);
Array.add (128);

//Traverse the collection
//Use method Size+get to traverse
for (int i = 0; i < array.size (); i++) {
System.out.println ( Array.get (i));
}
}
}


# # #14ArrayList补充方法
* A:arraylist Supplemental method
* A:ADD (int index, stored element) adds an element to the specified index
* B:set (int index, modified element) will specify the elements of the index to be modified
* C:remove (int index) Delete elements on the specified index
* D:clear () empties all elements in the collection
* B: Case code
Import java.util.ArrayList;
public class arraylistdemo_3{
public static void Main (string[] args) {

arraylist<integer> array = new arraylist<integer> ();
Array.add (1);
Array.add (2);
Array.add (3);
Array.add (4);

On index 2, add element 7
Array.add (2,7);

Change the elements on the 0 index to 10
Array.set (0,10);

The element on the 4 index is removed
Array.remove (4);

Array.clear ();

Traversing using the method Size+get combination
for (int i = 0; i < array.size (); i++) {
System.out.println (Array.get (i));
}
}
}

# # #15随机点名器案例分析
* A: Case Study of the random name register
The class randomly finds a classmate and prints the student's personal information.
We analyze this case to obtain the following analysis results:
1. Store the class information (name, age)
Replace the container with a collection, the student type is stored in the collection
2. Print the information (name, age) of each person in the class
Iterating through the collection
3. Within the total class size, randomly generate a random number to find the corresponding student information (name, age) of the random number
The random name register is clearly divided into three functions. If you write the code of multiple independent functions together, the code is relatively lengthy, and we can encapsulate it into a method for different functions, separating out the complete and independent functionality.
In the memory of the name of the students, if you define a variable for each student name storage, there will be too many isolated variables, it is difficult to hold all the data at once. At this point, we use ArrayList collection to solve the storage problem of multiple student information


# # #16随机点名器代码实现
* A: Random Register case code
/*
Random register, set improvement (student's name and age)
In reality, students are the things that describe students ' things in the form of defined classes.
Properties: Name, age

Name stores an array, replacing the container with a collection
String[] s = {"", ""};
Collection, is the name of the student stored? The student type should be stored

Storage students:
Student type, stored in the collection
Overview: Iterating through Collections
Random: Random number, as index, to find elements in the collection
Three functions, shared data, collection container,
Define three methods, you must pass a collection of parameters
*/
Import java.util.ArrayList;
Import Java.util.Random;
public class callname{
public static void Main (string[] args) {
Defines a collection that stores the Studentname type variable
ArrayList <StudentName> array = new arraylist<studentname> ();
Invoke Add method
Add (array);
Call Traversal Collection
Printarraylist (array);

Randomstudentname (array);
}
/*
Random number, as the index of the collection, to find the element in the collection
*/
public static void Randomstudentname (arraylist<studentname> array) {
Random r = new Random ();
int number = R.nextint (Array.size ());
Random number, index, to get in collection
Studentname s = array.get (number);
System.out.println (S.name + "" +s.age);
}

/*
Overview of student information, traversing the collection
*/
public static void Printarraylist (arraylist<studentname> array) {
for (int i = 0; i < array.size (); i++) {
Collection. Add (SN1) SN1 is a studentname type variable when the collection is stored
Gets the collection when the. Get method, what is obtained, or the Studentname type variable
Studentname s = array.get (i);
System.out.println (s.name+ "" +s.age);
}
}

/*
Define methods for storing student names and ages
Create a studentname type variable, stored in the collection
*/
public static void Add (arraylist<studentname> array) {
To create a studentname type variable
Studentname SN1 = new Studentname ();
Studentname SN2 = new Studentname ();
Studentname sn3 = new Studentname ();
Studentname sn4 = new Studentname ();
Studentname sn5 = new Studentname ();

Sn1.name = "Zhang 31";
Sn1.age = 201;

Sn2.name = "Zhang 32";
Sn2.age = 202;

Sn3.name = "Zhang 33";
Sn3.age = 203;

Sn4.name = "Zhang 34";
Sn4.age = 204;

Sn5.name = "Zhang 35";
Sn5.age = 205;

Store the Studentname variable in the collection
Array.add (SN1);
Array.add (SN2);
Array.add (SN3);
Array.add (SN4);
Array.add (SN5);
}
}


# # #17库存案例分析加入集合
* A: Inventory case analysis Join the collection
* A: See \day06\day06 (Object-oriented \day06_source\ object memory graph. Jpg

# # #18库存案例添加商品信息
* A: Case code
/*
definition,. Description of Product class
Product 4 properties
Product Name Size price inventory
String double double int

Definition Class, class name Goods
This type of variable, stored in the collection
*/
public class goods{
//define the product name
String brand;
Size property
Double size;
Price attribute
Double prices;
Inventory attribute
int count;
}

/*
Implement Inventory Management cases:
1. Store product Information
Store Commodity type variables
Store variables of commodity type into the collection
*/
Import java.util.ArrayList;
Import java.util.*;
public class shopp{
public static void Main (string[] args) {
Create ArrayList collection, store goods type
arraylist<goods> array = new arraylist<goods> ();
Call method to add product information
Addgoods (array);
}

/*
Define methods to store information about an item in a collection
Collections are shared data for all methods, parameter passing
*/
public static void Addgoods (arraylist<goods> array) {
Create a variant of type goods variable
Goods G1 = new Goods ();
Goods g2 = new Goods ();
G1.brand = "MacBook";
G1.size = 13.3;
G1.price = 9999.99;
G1.count = 3;

G2.brand = "Thinkpad";
G2.size = 15.6;
G2.price = 7999.99;
G2.count = 1;

Variables of type goods, stored in the collection
Array.add (G1);
Array.add (G2);
}
}


# # #19库存案例查看库存清单
* A: Case code
/*
Implement Inventory Management cases:
1. Store product Information
Store Commodity type variables
Store variables of commodity type into the collection

2. View inventory List
Traverse the collection to get the goods type variable stored in the collection
Output properties for each goods type
Calculate sum: Total inventory, total amount
*/
Import java.util.ArrayList;
Import java.util.*;
public class shopp{
public static void Main (string[] args) {
Create ArrayList collection, store goods type
arraylist<goods> array = new arraylist<goods> ();
Call method to add product information
Addgoods (array);
}

/*
Define methods, view inventory lists, traverse collections
*/
public static void Printstore (arraylist<goods> array) {
Output table Header
System.out.println ("----------Store stock list----------");
SYSTEM.OUT.PRINTLN ("Brand model size price Stock Number");
Define variables, save total stock count, and total amount
int totalcount = 0;
Double Totalmoney = 0;
Iterating through the collection
for (int i = 0; i < array.size (); i++) {
Get (index) Gets the elements in the collection, stores the goods class, and gets the goods type.
Accept the Get method result using the goods type variable
Goods g = Array.get (i);
System.out.println (g.brand+ "" +g.size+ "" +g.price+ "" +g.count ");
TotalCount = Totalcount+g.count;
Totalmoney = Totalmoney + g.count*g.price;
}
SYSTEM.OUT.PRINTLN ("Total number of stocks:" +totalcount);
SYSTEM.OUT.PRINTLN ("Total Commodity inventory Amount:" +totalmoney);
}

/*
Define methods to store information about an item in a collection
Collections are shared data for all methods, parameter passing
*/
public static void Addgoods (arraylist<goods> array) {
Create a variant of type goods variable
Goods G1 = new Goods ();
Goods g2 = new Goods ();
G1.brand = "MacBook";
G1.size = 13.3;
G1.price = 9999.99;
G1.count = 3;

G2.brand = "Thinkpad";
G2.size = 15.6;
G2.price = 7999.99;
G2.count = 1;

Variables of type goods, stored in the collection
Array.add (G1);
Array.add (G2);
}
}

# # #20库存案例修改库存清单及测试代码的实现
* A: Case code
/*
Implement Inventory Management cases:
1. Store product Information
Store Commodity type variables
Store variables of commodity type into the collection

2. View inventory List
Traverse the collection to get the goods type variable stored in the collection
Output properties for each goods type
Calculate sum: Total inventory, total amount

3. Modify the inventory of the product
Collection traversal, gets the goods type variable stored in the collection
Variable calls the property of the goods class count, and the value is modified (keyboard input)
*/
Import java.util.ArrayList;
Import java.util.*;
public class shopp{
public static void Main (string[] args) {
Create ArrayList collection, store goods type
arraylist<goods> array = new arraylist<goods> ();
Call method to add product information
Addgoods (array);
Into the dead loop.
while (true) {
Invoke the Select function method to get the function number entered by the user
int number = Choosefunction ();
Judgment on serial number, if = 1 Enter view inventory function = 2 Go to modify inventory function =3 End
Switch (number) {
Case 1:
Go to view inventory, call the view inventory method, pass a collection of stored commodity information
Printstore (array);
Break

Case 2:
Go to modify inventory function, call the method to modify inventory, pass the collection
Update (array);
Break

Case 3:
return;

Default
System.out.println ("Without this function");
Break
}
}
}
/*
method definition, modifying inventory
The input of the keyboard, the value of the property in the goods, modified
*/
public static void update (arraylist<goods> array) {
Scanner sc = new Scanner (system.in);
Iterate through the collection to get each element in the collection
for (int i = 0; i < array.size (); i++) {
The collection method get gets the elements of the collection, the element type goods
Goods g = Array.get (i);
System.out.println ("Please enter the" +g.brand+ "in Stock Number");
Goods property, Count is modified
G.count = Sc.nextint ();
}
}
/*
Defines the method, implements the selection menu, the user selects the menu according to the function
*/
public static int choosefunction () {
System.out.println ("-------------Inventory management------------");
System.out.println ("1. View inventory list");
System.out.println ("2. Change the quantity of goods in stock");
System.out.println ("3. Exit");
System.out.println ("Please enter the number of actions to be performed:");
Scanner sc = new Scanner (system.in);
int number = Sc.nextint ();
return number;
}

/*
Define methods, view inventory lists, traverse collections
*/
public static void Printstore (arraylist<goods> array) {
Output table Header
System.out.println ("----------Store stock list----------");
SYSTEM.OUT.PRINTLN ("Brand model size price Stock Number");
Define variables, save total stock count, and total amount
int totalcount = 0;
Double Totalmoney = 0;
Iterating through the collection
for (int i = 0; i < array.size (); i++) {
Get (index) Gets the elements in the collection, stores the goods class, and gets the goods type.
Accept the Get method result using the goods type variable
Goods g = Array.get (i);
System.out.println (g.brand+ "" +g.size+ "" +g.price+ "" +g.count ");
TotalCount = Totalcount+g.count;
Totalmoney = Totalmoney + g.count*g.price;
}
SYSTEM.OUT.PRINTLN ("Total number of stocks:" +totalcount);
SYSTEM.OUT.PRINTLN ("Total Commodity inventory Amount:" +totalmoney);
}

/*
Define methods to store information about an item in a collection
Collections are shared data for all methods, parameter passing
*/
public static void Addgoods (arraylist<goods> array) {
Create a variant of type goods variable
Goods G1 = new Goods ();
Goods g2 = new Goods ();
G1.brand = "MacBook";
G1.size = 13.3;
G1.price = 9999.99;
G1.count = 3;

G2.brand = "Thinkpad";
G2.size = 15.6;
G2.price = 7999.99;
G2.count = 1;

Variables of type goods, stored in the collection
Array.add (G1);
Array.add (G2);
}
}

Java Foundation Sixth Day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.