DOC PREVIEW
UNC-Chapel Hill COMP 401 - An Overview of the Java Language

This preview shows page 1-2-3-4-5-34-35-36-37-38-69-70-71-72-73 out of 73 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 Introduction2 A first program3 Program annotations: Comments4 Java Identifiers4.1 Identifier Names5 Java primitive data types5.1 Integers5.2 Real numbers5.3 Booleans5.4 Character5.5 Variables5.6 Constants5.7 Expressions6 Java statements6.1 Assignment statement6.2 The String class6.3 Simple input and output6.3.1 Output6.3.2 Input6.3.3 Program 0 revealed6.4 Arrays6.5 Java Control Structures6.5.1 Selection: conditional execution6.5.2 Iteration: Repeated execution6.5.3 Counting loops6.5.4 The do loop6.5.5 The continue statement6.5.6 Scope of variable names7 Methods7.1 Method syntax7.2 Overloading methods8 Object oriented programming8.1 Constructors8.2 Special methods8.2.1 toString method8.2.2 Equals method8.3 An alternate implementation of Rectangle8.4 Private methods8.5 Static methods and class variables8.6 What's really going on here?8.7 Another example: the Rational class8.8 Another look at arrays8.8.1 Array length8.8.2 The args array8.9 Objects as parameters8.10 Another look at Strings8.10.1 String tools8.11 Vectors8.12 Wrapper classes8.13 Interfaces (required for Chapter 8)9 Exceptions, and robust programs (required for Chapter 3)9.1 Robust programs9.2 Java exceptions9.3 More on catching exceptions.9.4 The finally clause9.5 Throwing an exception9.6 Creating new exceptions10 Reading and writing a text file11 A final wordChapter 1An Overview of the Java Language1 IntroductionThe purpose of this chapter is to give you a brief introduction to the Java language. It isnot a tutorial on how to program, and even less a guide to how to program well. We'llspend a large part of this book discussing how programs should be written, but the aim ofthis chapter is simply to introduce you to the most basic syntax and intuitive semantics ofJava. If you already know Java, you can probably just skim the chapter. We assume thatyou already have experience programming in a high level language (such as C, C++ orPascal), including such basic programming concepts as variables, assignment, simple inputand output (I/O), conditional execution, loops, and subroutines (called methods in Javaparlance). If you don't know how to program, this chapter won't help you. We take a spiral approach to introducing Java concepts, introducing just enough at first toget you going, and adding details later. This approach works well pedagogically, butmakes for a frustrating reference. We recommend that you have an introductory Java texthandy that contains a detailed reference section.It is not necessary to read and master this chapter all at one time. We have concentratedthe Java language material in this one chapter rather than spreading it out through thebook. You should read the first sections of this chapter before going on to Chapter 2, butother sections can wait until you are ready to read later chapters. Each section in thischapter is keyed to later chapters. Similarly, each chapter has a prerequisite sectiontelling which sections of this chapter are required reading. So, let's begin.2 A first program The simplest Java program is a sequence of zero statements; interesting programs arenecessarily longer. We'll begin by introducing the parts of the language necessary for the© 2001 Donald F. Stanat and Stephen F. WeissChapter 1 Java Overview Page 2following program.1 Look over the program. Some of it may look familiar; other partslook like magic incantations (and in some ways they are). Program output is shown on thenext page./*********************************** Program 0: A very gentle introduction to Java Description: This program greets the user, requests two integers, then displays the sum, product, and quotient of the integers.*/import java.io.*;public class Program0{public static void main (String[] args) throws IOException{BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));int num1; // Holds the two integers entered.int num2;String name; // Holds name.// Greet the user.System.out.println("Hello");System.out.print("What is your name? -> ");name=stdin.readLine();System.out.println("Hello "+name);// Request and read two integers.System.out.print("Please enter the first integer. -> ");num1=Integer.parseInt(stdin.readLine());System.out.print("Please enter the second integer. -> ");num2=Integer.parseInt(stdin.readLine());// Display sum, product and quotient of two integers.1The programs of this chapter are meant to illustrate the characteristics of the Java language, andalthough we will make an effort not to violate rules of good programming practice, many of theprograms will be neither exemplary nor interesting. 1/14/2019 1/14/2019Chapter 1 Java Overview Page 3System.out.println("\nThe sum of "+num1+" and "+num2+" is "+(num1+num2));System.out.println("The product of "+num1+" and "+num2+" is "+(num1*num2));System.out.println("The quotient of "+num1+" and "+num2+" is "+(((double)num1)/num2));// Wait before quitting.System.out.println("\nPlease hit enter to quit.");name=stdin.readLine();System.out.println("\nEnd of job");} // End of main method.} // End of Program0 class.This program contains comments that are directed to the reader of the program,declarations of variables, expressions that calculate values, assignment statements thatstore the values of expressions in variables and input and output statements.3 Program annotations: CommentsComments are program annotations that are intended to aid a reader in understanding aprogram -- what it does, how it works, limitations, etc. Comments do not affect programexecution in any way. 1/14/2019 1/14/2019Chapter 1 Java Overview Page 4Java comments occur in two distinct forms. A comment of any size and covering anynumber of lines can be inserted anywhere in a Java program by enclosing the commenttext within /* and */, which serve as left and right brackets for the comment. Forexample: /* Read in the radius of a circle and the length of a side of a square and display the areas of the circle and the square. */Comments delimited by the /* and */ can, in fact, occur in the midst of a programstatement; thussam = george /* This is not usually a good idea. */ + bill;will have exactly the same effect assam = george + bill;Note that comments that are begun with /* must be explicitly terminated with a */. The second comment form is commonly used for brief comments. Two consecutive slashes(//) signal the beginning of


View Full Document

UNC-Chapel Hill COMP 401 - An Overview of the Java Language

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download An Overview of the Java Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view An Overview of the Java Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view An Overview of the Java Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?