Introduction to Java Programming Ideas (Charades 1.0)

Source: Internet
Author: User

First, Introduction

 This chapter will use a simple crossword puzzle to give the reader a brief introduction to the JAVA Object-oriented part of the knowledge and implementation ideas.

Introduction of object-oriented thought

  To understand what is object-oriented, we need to address the following issues (2.1 ):

    • What is object-oriented?
    • An object-oriented feature (or representation).
    • Why object-oriented (it differs from process-oriented).
    • What does object-oriented do?

It is well known that the computer programming language starts from the imitation of the machine, that is, in the form of code to control the machine to accomplish the task or effect that people want to achieve, so all programming languages provide abstract mechanism.

And this abstraction can actually be thought of--the complexity of the problems that people can solve depends directly on the type and quality of the abstraction. Among them, the so-called "type" means "What do we abstract?" ”。

    1. Assembly language: It is a slight abstraction of the underlying machine (his commands, which are often read-and-write mobile storage involving data, are straightforward).
    2. C Language: is the abstract of assembly language, that is, the so-called "command-style" language. (Beginning to approach human syntax, such as the if statement).

But the main abstraction in both languages still requires a computer structure to solve the problem. Although the C language can be defined by structs to Achieve the so-called object-oriented effect. However, the essence of abstraction is based on the structure of the computer, this from the bottom of the drive, you need to #define macro definition of Physical address mapping can be seen.

So the problem is that this computer-based language, due to his own specific nature (data definition and function invocation), requires our programmers to establish the association between the machine model and the actual problem to be solved. However, it is often laborious to build this mapping, as we can see from the number of embedded bottom engineers in the industry.

Because of these non-universality, the object-oriented method of programming thinking is finally spawned.

In short, when we deal with the problem of being solved, the main abstraction is not based on the computer structure, but on our problem itself. The essence of this idea is that the program can adapt itself to a particular problem by adding new objects. But this OOP idea is still in contact with the computer: Each object looks a bit like a computer-it has a state and has operations, and the user can ask the object to perform these operations.

For this reason, Booch presents a more concise description of the object: the object has state, behavior, and identity. This means that each object can have internal data (which determines the state of the object) and methods (what behavior they will produce), and each object has a unique identity, meaning that each object is unique in memory.

Objects are unique, but they always have similar characteristics and behaviors, such as sharks and grass carp, which belong to fish, but they are not the same object. For this reason, in the idea of object-oriented programming, similar concepts are introduced-objects that have different states during program execution and others that are similar are grouped into object classes, which is the origin of class.

Class(Class) is actually a data type because it is a collection of objects that describe similar attributes (internal data elements) and behaviors (functions). Creating this kind of abstract data type is one of the basic concepts of object-oriented programming. You can create a variable of a certain type (in object-oriented, called an instance or an object), and then manipulate the variables (referred to as sending messages and requests).

So, while we actually do the creation of new data types in object-oriented programming, in fact all object-oriented programming languages use class to represent data types. The difference is that programmers can define classes to accommodate problems, rather than being forced to use existing data types that represent the storage units in the machine. That is, we have the freedom and flexibility to define the type of data we need.

Although the object-oriented approach is so free and universal, its application can transform a large number of problems into simple solutions. Once a class is created, you can create arbitrary objects of the class as you like, and then manipulate them as if they were elements of the problem that you are actually solving. But in fact, one of the challenges of object-oriented programming is that there are one by one mappings between the unresolved elements in the problem space and the objects we solve in the problem space.

Therefore, we will have a question: how can we obtain useful objects? To solve this problem, we can start from the meaning of the object, the meaning of the object is actually to make the object accomplish certain tasks, such as the completion of the path selection and so on. Specifically, each object satisfies only certain requests, which are defined by the interface of the object, which is the type that determines the interface, and the class is a specific implementation of the interface. For example, if a type defines his interface as an integer, the request that he can accept must be an integer data, and the processing behavior after receiving the data is only an implementation of the request, and the class containing the data types and behaviors is the specific implementation form of this interface-oriented.

The interface determines the requests that can be made to a particular object, but there must be code in the program that satisfies the request, and the code is implemented with the hidden data in order to make it. From a process-oriented point of view, it's not too difficult to understand: "Send a Message" to an object (generating a request), the object will know the purpose of the matter, and then execute the corresponding code.

Let's take an example of a TV-on to describe the process, first of all, the name of our type / class is TV, we need to create a specific name at this point, otherwise we cannot specify the operation when multiple references are in progress. Then we can make a request to the TV object: Open it, close it.

You will create a TV object in the following way : Define the reference (it) for this object, and then call the new method to create the object of that type. In order to send a message to an object, we need to declare the object name and connect a message request with a dot symbol.

New Light (); It.on ();

Finally, since the object-oriented idea is based on the problem itself, it will inevitably produce the following considerations:

1, if I can extract the problem from the appearance, then what kind of object can immediately solve my problem? (The simplest is to say what to enter, what to output, one step to solve)

2. Do some of the objects I need exist or do they look like if they don't exist?

3. What kind of services are required for them to provide?

4. What objects do they need to fulfill their obligations? (What requests the object needs to receive)

Figure 2.1 Introduction to Ideas

First, build their own applications

3.1 Requirements:

  Build a simple charades game 1.0. Requirements:

    • Guess the number range in the 1~100 Integer.
    • Each user has 10 opportunities.

3.2 How to grasp the object-oriented

  3.2 shown, as JAVA The programming idea is object-oriented. In other words, abstraction is the problem itself, so it determines the direction of thinking-demand-oriented.

And in this one we can extract a few key requirements from the 3.1. We first need to consider providing the player with an interactive platform (input-output,I/o), allowing the user to enter numbers on the controls, and to consider the feedback of the game. Next, we need to simulate a random number generator to generate the random number values we need.

Now if we can get The random number between 1~100, then we need to implement the game rules. We need conditional statements such as while and if to describe the rules.

Rule: Each user has 10 chances to guess a random integer between a 1~100.

Finally, we need a counter to count the number of user operations. Yes, and the most important thing is whether the user's input conforms to the rules.

To do this, we can abstract out four parts:

1. An interface that can read and write to the console.

2, a generator that generates random numbers between 1~100.

3. A comparator that compares the user input data correctly and generates a number equal to the generator.

4, a cycle counter.

After the object is defined, we need to define the data structure that defines the type and the way in which it is stored in the program. Because we need integer variables in this program, we are all defined as int types:

    • type int variable num: The total number of times a user guesses a number.
    • type int Data temp: Used to store numbers generated by the number generator.
    • int data type input: Used to store user input data.

Finally, we begin to define the function module as the Generator module (Generate()), the I/O interaction Module (Scanner()), and the Judgment comparison loop module.

3.2 Program Thinking Process

3.3 Algorithmic Flow

3.4 Specific implementation code

package Numgame;import Java.util.scanner;public class Numgame {/** * A simple guess number game * @param Num represents the number of user guesses. * @param temp Store Generator generated random number * @param input user entered number */public static void Main (string[] args) {int num = 0; Scanner s = new Scanner (system.in); int temp = generate (); System.out.println ("Welcome to the guessing numbers game!") ");//Game compare loop function body structure while (num<=10) {//Determine number of users remaining. if (num = = ten) {System.out.println ("You have exceeded 10 chances!") Please come back again.  "); break;} Receive input and determine if the input conforms to the rule System.out.println ("You also have" + (10-num) + "Times! Please enter your number (1~100): "); int input = S.nextint (); if (input<1| | input>100) {System.out.println ("Please start again!") Enter the correct number (1~100)! "); break;} Compare user input numbers with generated random number else if (Input > Temp) {System.out.println ("the number you entered is big!") "); num=num+1;} else if (Input < temp) {System.out.println ("the number you entered is small!") "); num=num+1;} else if (input = = temp) {System.out.println ("you won! Thank you for participating ~ ~ "); break;}} S.close ();}  /** * Generate random number between (1~100) * @param return store random number */private static int generate () {int temp = (int) (Math.random () *100+1); return temp;}} 

  

Introduction to Java Programming Ideas (Charades 1.0)

Related Article

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.