The knife first try to feel the charm of Programming

Source: Internet
Author: User

PS: I even saw some programming Videos released by foreign universities on the Internet. After a simple look, when their teachers took the course, at the beginning, you may use the knowledge you want to talk about to write something for demonstration to arouse students' interest. Next, we will write a small system that will first experience the charm of programming.

Okay, let's get started! The purpose of a small system is to input the user to select the shape of the painting and print a piece of text (which describes the shape of the painting ).

Consider that there may be multiple types of painting images, so as to define an abstract class shape, and the specific shape can be implemented in the method.

Package chapterone; // drawing abstract class public abstract class shape {public abstract void draw ();}
View code
// Draw a circle for the drawing instance package chapterone; public class circle extends shape {@ override public void draw () {system. Out. println ("draw a circle !!! ");}}
View code
// Draw a straight line for the drawing instance package chapterone; public class line extends shape {@ override public void draw () {system. Out. println ("draw a straight line !!! ");}}
View code
// Draw a rectangle for the drawing instance package chapterone; public class rectangle extends shape {@ override public void draw () {system. Out. println ("draw a rectangle !!! ");}}
View code

The user, the so-called main method, and the entry of the program

Package chapterone; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; public class panel {public static void main (string [] ARGs) {panel Panel = new Panel (); panel. selectshape ();} // select the image to be painted public void selectshape () {system. out. println ("there are three options for drawing:"); system. out. println ("1. circle 2. straight Line 3. rectangle "); system. out. println ("Enter the number of the image you will draw !!! "); Bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); int shapetype = 0; try {shapetype = integer. parseint (BR. readline ();} catch (numberformatexception e) {New runtimeexception ("type formatting exception");} catch (ioexception e) {e. printstacktrace ();} shape = NULL; Switch (shapetype) {Case 1: Shape = new circle (); break; Case 2: Shape = new line (); break; case 3: Shape = new rectan Gle (); break;} If (shape = NULL) {system. Out. println ("the image you selected does not exist. Exit the system !!! ") ;}Else {shape. Draw (); // image selected for painting }}}
View code

Run the main method and select the drawing to print the corresponding text.

Note: If you do not understand the above, you can ignore it first and appreciate the charm of programming. After all, it just imitates the code in the book. When you are learning, step by step, you can understand the meaning of the content and understand the meaning of the content... Of course, not all people are suitable. The key lies in whether you think you can do it or not and find your own method. In this way, learning is becoming more and more active and interesting. I think so...

Now the preparation is complete. I will learn Java later (I chose Java ).

Finally, I would like to talk about the previous article every day or at least two days. The result is that the current project is coming to an end and the code is changed every day, the changed eyes cannot be opened, and the busy brain won't be able to change. I feel a little confused, not me. No matter how long I have time, I have to keep writing it, why do we need to write this foundation? On the one hand, we need to learn about new things with a warm understanding, and on the other hand, we need to improve our writing skills. please correct me when using this article to practice shortcomings...

The knife first try to feel the charm of Programming

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.