Unformatted text preview:

Just Enough Java Jan 14 2019 What is Java Java is a programming language a language that you can learn to write and the computer can be made to understand Java is currently a very popular language Java is a large powerful language but it is not simple Compared to C Java is elegant 2 Structure of a Java program A simple program consists of a single package A package contains one or more classes A class contains one or more fields and methods Package directory folder A method contains declarations and statements Classes and methods may also contain comments We ll begin by looking at the insides of methods packages classes fields methods declarations statements 3 Java structure and Eclipse A workspace is where Eclipse keeps projects When you use Eclipse to create a project a single program it creates a directory with that name in your workspace When you use Eclipse to create a class it creates a file in the project directory that has the same name as the class When you create a class and don t specify a package name Eclipse complains The use of the default package is discouraged Ignore this message for now 4 Simple program outline class MyClass main metho d public static void main String args new MyClass run anothe r metho d void run some declarations and statements go here this is the part we will talk about today Notes The class name MyClass must begin with a capital main and run are methods This is the form we will use for now Once you understand all the parts you can vary things 5 Syntax and semantics Syntax is the grammar of the language The syntax of Java is large but finite Syntax must be absolutely correct The computer will point out every syntax error Error messages may be helpful or misleading Semantics is the meaning of your program Semantic errors cause your answers to be wrong You may or may not get error messages 6 Two aspects of Java Java has syntax and semantics This is where you begin It is possible to learn everything about Java s syntax and semantics We will cover most of Java s syntax and semantics Java comes with many built in packages Packages are sort of like vocabulary bundles To be good at Java you need to learn many packages There are more Java packages than you can ever learn 7 What you need to know You need to be able to Read in data for now numbers Save numbers in variables We will use while statements Print output We will use if statements Do something repeatedly We will use assignment statements Test whether or not to do something We will use declarations to create variables Do arithmetic on numbers to get new numbers We will use a Scanner for this We will use System out print and System out println Not absolutely necessary but very helpful We will use methods 8 Declarations statements comments A declaration gives some information to the computer A statement tells the computer to do something Statements should really be called commands Comments are ignored by the computer they are explanations of your program for human beings to read A method may contain declarations statements and comments 9 Variables A variable is a box that holds data Every variable has a name Examples name age address isMarried Variables start with a lowercase letter In multiword variables each new word is capitalized Every variable has a type of value that it can hold For example name might be a variable that holds a String sequence of characters age might be a variable that holds an integer value isMarried might be a variable that holds a boolean true or false value 10 Some Java data types In Java the four most important primitive simple types are int variables hold integer values double variables hold floating point numbers that is numbers containing a decimal point boolean variables hold a true or false value char variables hold single characters Another important type is the String A String is an Object not a primitive type A String is composed of zero or more chars 11 Declaring variables Every variable that you use in a program must be declared in a declaration The declaration specifies the type of the variable The declaration may give the variable an initial value Examples int age int count 0 double distance 37 95 boolean isReadOnly true String greeting Welcome to CIT 591 String outputLine 12 Reading in numbers First import the Scanner class import java util Scanner Create a scanner and assign it to a variable Scanner scanner new Scanner System in The name of our scanner is scanner new Scanner says to make a new one System in says the scanner is to take input from the keyboard Next it s polite to tell the user what is expected System out print Enter a number Finally read in the number myNumber scanner nextInt If you haven t previously declared the variable myNumber you can do it when you read in the number int myNumber scanner nextInt 13 Printing There are two methods you can use for printing System out println something System out print something This prints something and ends the line This prints something and doesn t end the line so the next thing you print will go on the same line These methods will print anything but only one thing at a time You can concatenate things with the operator Example System out println There are appleCount apples and orangeCount oranges 14 Program to double a number import java util Scanner public class Doubler public static void main String args new Doubler run private void run Scanner scanner int number int doubledNumber scanner new Scanner System in System out print Enter a number number scanner nextInt doubledNumber 2 number System out println Twice number is doubledNumber 15 Assignment statements Values can be assigned to variables by assignment statements The syntax is variable expression The expression must be of the same type as the variable The expression may be a simple value or it may involve computation Examples name Dave count count 1 area 4 0 3 0 3 1416 radius radius isReadOnly false When a variable is assigned a value the old value is discarded and totally forgotten 16 One line comments A comment is a note to any human reading the program comments are ignored by Java A single line comment starts with and goes to the end of the line A comment may be put after a statement on the same line to say something about that one statement A comment may be put on a line by itself to say something about the following statements Example Swap the values of x and y temp x save old value of x in temp x y replace old value of x with y y temp


View Full Document

Penn CIT 591 - Just Enough Java

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view Just Enough Java 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 Just Enough Java 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?