Recalling Java memoirs (2): Java 02 class and object, Java 02

Source: Internet
Author: User

Recalling Java memoirs (2): Java 02 class and object, Java 02


Class: subjective abstraction. Object Templates Can be instantiated into objects-defining a template, which cannot be directly used.

--- To use classes, you must create them as objects.

-- Abstract The actual objects (emphasize the similarities between objects and ignore different processes)

 

-- Use abstract results: the template is embodied (instantiated) and operated with the instantiated results

 

Define the format of a class in habits

Package XXX;

Import XXX;

[Modifier] Class name {

Attribute: represented by a variable (basic data type)

Behavior: Use Method Representation (basic statement method)

Constructor)

}

 

-- Relationship between class, object, and instance 3

Class: an object template that is instantiated into an object (a space template)

Object: an individual of a class (referenced pointer)

Instance: Implemented object (memory space)

Student s = new Student ();

 

 

Job:

1. Generate 100 random integers between 0 and 100, and then judge the random integers.

Which integers are prime numbers and which are not?

 

Program analysis: a prime number is an integer that can only be divisible by 1 and itself. Judge 1

Whether integer n is a prime number is to determine whether integer n can be divided by 1 and itself.

If all integers cannot be divisible, n is the prime number.

During program design, I can start from 2 to 1/2 of the integer n, and use I in sequence.

Remove the integer to be determined, as long as there is a situation where the number can be divisible.

Make sure that the integer to be judged is not a prime number; otherwise, it is a prime number.

 

[Procedure 2]

2. Print n rows of pyramid pattern on the screen. For example, if n = 5, the pattern is as follows:

*

***

*****

*******

*********

 

3. Reverse the elements in an array.

Storage space, which can only be changed on the original array.

 

4. Create an array class. The initialization size of the array is 10 elements. When the elements in the array are filled

The capacity is automatically expanded by 10. Write a program for testing and above!

 

5. (1) Write a class named Animal, which has two private attributes, name

(Representing the animal name), and legs (representing the number of animal legs );

Public access is required for two private properties. And provide

Two overloaded constructor methods. One requires two parameters.

The two parameters assign values to the private attribute name and legs. The other has no parameters,

By default, the name is assigned AAA, and the legs is assigned 4. The second parameter is required.

To call the first constructor. This class has two duplicates.

The move () method, one of which has no parameters. Output a row on the screen.

Text: XXXMoving !! (XXX is the name of the animal); the other must

An int parameter n is required, and XXX Moving n meters are output on the screen !!

(2) Write a Fish class that inherits from the Animal class and provides a constructor,

This constructor requires a parameter name and assigns the default value 0 to legs.

Class also needs to overwrite the non-parameter move () method in the Animal class, and the output is required

: XXXSwimming !!

(3) Write a Bird class that inherits from the Animal class and provides a constructor,

This constructor requires a parameter name and assigns the default value of 2 to legs.

Class also needs to overwrite the non-parameter move () method in the Animal class, and the output is required

: XXXFlying !!

(4) Write a class Zoo, define a main method, and generate

Dry Animal, Fish and Bird. And call their attributes and methods.

 

 

6. Write a class Person, which includes the following attributes: String name; int age;

Boolean gender; Person partner (spouse ). Write 1 for the Person class

The marry (Person p) method indicates that the current object is married to p.

If you get married, the congratulation message is output; otherwise, the reason for not getting married is output. It must be in another

Class to test the above program.

You cannot get married in the following cases:

1. Same-sex;

2. Not married, male <24, female <22;

3. One party is married.



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.