The Java Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
Java Programming 2: The Java Programming Language Lesson 1: Java Pro gramming Co nst ruct s Before You Start: Working Sets Selecting a Working Set Windows Settings Fundamental Programming Constructs Basic Algorithm Constructs What and Where? Applications Our First Application The System Class A Closer Look at main Documentation and Comments Lesson 2: T o o ls o f Pro gramming: Applicat io ns and Co mpilers Application GUIs More on Programming Constructs Statements and Expressions Primitive Data Types Valid Identifiers (aka. Variable Names) Lesson 3: Primit ive Dat a T ypes, Variables, and Expressio ns Bits and Pieces in Memory Variables Primitive Data Types Strings Literals Memory: variable names and values Optional Reading: The Bus Lesson 4: Out put : St rings, Fo nt s, and Numbers Strings Investigating Strings Using Strings Manipulating Strings Fonts Numbers Lesson 5: Arit hmet ic Operat io ns: T he Basics Calculations and Operators Variables and a Program to Dissect Defaults Added Attractions Increment Statements Lesson 6: Arit hmet ic Operat io ns: Precedence and Operand T ypes Executing Tasks in the Correct Order Operators and Precedence Remainder Precedence Operand Types Type Conversion/Casting More on String Concatenation Formats Lesson 7: Decisio ns: Lo gic Logic: and, or, not Examples Using Logic Nested Ifs Versus Logic Statements Logic Statements Versus If Statements Lesson 8: Decisio ns: Dangling and Swit ching Matching Who Gets What Dangling Elses Object equivalence The Instance of Operator Switch Statements What's next? Lesson 9: Int ro duct io n t o Arrays What are Arrays? Some Fab Examples Index Possibilities Syntax and Java Implementation for Arrays Declaring a Variable to Refer to an Array Memory Lesson 10: Lo o ps Repetition: for Loops Introduction to Loops for start stop increment Lesson 11: Creat ing Arrays Creating, Initializing, and Accessing What are Arrays again? Creating Arrays (revisited) Shortcut for Creating Arrays Lesson 12: T wo -Dimensio nal Arrays More About Arrays Working With Two-Dimensional Arrays Ragged Arrays Using Arrays Copying Arrays Common Mistakes Lesson 13: T he Ot her Lo o ps Repetition: while, do-while Enhanced For Loops while do-while Infinite Loops Optional For Loop Branching Statements General Lesson 14: T racing Co de How Did I Get Here? Arrays for Methods--Reminder Always Look Before Passing (or Hiring) Nested for Tracing do-while Tracing Decisions More About Flow Control Summary of Control Constructs Branching Repetition/Loops Copyright © 1998-2014 O'Reilly Media, Inc. This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information. Java Programming Constructs Welcome to OST's Java 2 course! Course Objectives When you complete this course, you will be able to: demonstrate knowledge of basic algorithm constructs in Java. develop and compile Java applications that utilize primitive data types, statements, expressions, and GUIs. output and manipulate strings, fonts, and numbers. apply decision logic and operational precedence to Java code. implement multidimensional arrays, loops, and branching statements. trace code for better software quality. When you complete this lesson, you will be able to: Lesson Objectives Identify the control constructs used in structured programming. use the main method to start a Java application. access Java documentation. create a new Java project with a class that prints specific text. In this course, you'll learn more in-depth concepts and syntax of the Java Programming language. Throughout this course, you'll learn by building examples using the Eclipse Java Development Environment which is supplied as a Learning Sandbox. Completion of this course gives you a basic understanding of Object Oriented techniques in Java as well as using the Eclipse IDE. From beginning to end, you will learn by doing your own Java projects, within our Eclipse Learning Sandbox we affectionately call "Ellipse". These projects will add to your portfolio and provide needed experience. Besides a browser and internet connection, all software is provided online by the O'Reilly School of Technology. Learning with O'Reilly School of Technology Courses As with every O'Reilly School of Technology course, we'll take a user-active approach to learning. This means that you (the user) will be active! You'll learn by doing, building live programs, testing them and experimenting with them— hands-on! To learn a new skill or technology, you have to experiment. The more you experiment, the more you learn. Our system is designed to maximize experimentation and help you learn to learn a new skill. We'll program as much as possible to be sure that the principles sink in and stay with you. Each time we discuss a new concept, you'll put it into code and see what YOU can do with it. On occasion we'll even give you code that doesn't work, so you can see common mistakes and how to recover from them. Making mistakes is actually another good way to learn. Above all, we want to help you to learn to learn. We give you the tools to take control of your own learning experience. When you complete an OST course, you know the subject matter, and you know how to expand your knowledge, so you can handle changes like software and operating system updates. Here are some tips for using O'Reilly School of Technology courses effectively: T ype t he co de. Resist the temptation to cut and paste the example code we give you. Typing the code actually gives you a feel for the programming task. Then play around with the examples to find out what else you can make them do, and to check your understanding. It's highly unlikely you'll break anything by experimentation. If you do break something, that's an indication to us that we need to improve our system! T ake yo ur t ime. Learning takes time. Rushing can have negative effects on your progress. Slow down and let your brain absorb the new information thoroughly. Taking your time helps to maintain a relaxed, positive approach. It also gives you the chance to try new things and learn more than you otherwise would if you approach. It also gives you the chance to try new things and learn more than you otherwise would if you blew through all of the coursework too quickly. Experiment . Wander from the path often and explore the possibilities. We can't anticipate all of your questions and ideas, so it's up to you to experiment and create on your own. Your instructor will help if you go completely off the rails. Accept guidance, but do n't depend o n it . Try to solve problems on your own. Going from misunderstanding to understanding is the best way to acquire a new skill. Part of what you're learning is problem solving. Of course, you can always contact your instructor for hints when you need them. Use all available reso urces! In real-life problem-solving, you aren't bound by false limitations; in OST courses, you are free to use any resources at your disposal to solve problems you encounter: the Internet, reference books, and online help are all fair game. Have f un! Relax, keep practicing, and don't be afraid to make mistakes! Your instructor will keep you at it until you've mastered the skill. We want you to get that satisfied, "I'm so cool! I did it!" feeling. And you'll have some projects to show off when you're done. Before You Start: Working Sets We organize our projects in Working Sets. To create the working sets for this course, use the down arrow beside the Red Leaf icon on the toolbar. You will use the Java2_Lesso ns working set for the lessons, and the Java2_Ho mewo rk working set for the exercises after each lesson. Selecting a Working Set When you start working on a course, or if you just need to reset your perspective, you need to select the appropriate working set. Click the small down arrow beside the Red Leaf icon on the tool bar to see a list of course groups. For this course, select the Jave course group. If you do not see working sets in your Package Explorer, set the top-level elements to working sets: To configure working sets: The following dialog appears. Select the working sets you want and deselect the ones you don't want in the Package Explorer view, and click OK. The selected working sets now appear in the Package Explorer: To easily show just the working sets for this course, click the down arrow on the Sho w Wo rking Set s button in the Package Explorer, then select Java, and then Java2: If the working sets already exist in the Package Explorer, they will not be recreated, but the perspective will still change. Hooray! Let's get going on some more Java Programming! Windows Settings If you like, you can set your own Windows mouse, keyboard, and region; for example, if you are left-handed, you can switch the left and right button functionality on the mouse, or change date fields to use date formats for your local region. Click the down arrow on the Windows Settings button at the top right of the screen: We won't discuss all of the details of these dialog boxes, but feel free to ask your instructor if you have questions. Fundamental Programming Constructs The obvious reason that object-oriented programming languages use objects is due to the power that design principles such as inheritance, information hiding and polymorphism provide the programmer.