DOC PREVIEW
Penn CIT 591 - Just Enough Java

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Just Enough JavaWhat is Java?Structure of a Java programJava structure and EclipseSimple program outlineSyntax and semanticsTwo aspects of JavaWhat you need to knowDeclarations, statements, commentsVariablesSome Java data typesDeclaring variablesReading in numbersPrintingProgram to double a numberAssignment statementsOne-line commentsOther kinds of commentsMethodsOrganization of a classArithmetic expressionsBoolean expressionsString concatenationif statementsCompound statementswhile loopsMethod callsA complete programAnother complete programThe EndJan 14, 2019Just Enough Java2What 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 elegant3Structure of a Java programA simple program consists of a single packagePackage = directory = folderA package contains one or more classesA class contains one or more fields and methods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• statements4Java 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.5Simple program outline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 thingsclass MyClass { public static void main(String[ ] args) { new MyClass().run(); } void run() { // some declarations and statements go here // this is the part we will talk about today }}main methodanother method6Syntax 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 messages7Two 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 learn8What you need to knowYou need to be able to:Read in data (for now, numbers)We will use a Scanner for thisSave numbers in variablesWe will use declarations to create variablesDo arithmetic on numbers to get new numbersWe will use assignment statementsTest whether or not to do somethingWe will use if statementsDo something repeatedlyWe will use while statementsPrint outputWe will use System.out.print and System.out.printlnNot absolutely necessary, but very helpful:We will use methods9Declarations, 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 comments10Variables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) value11Some 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 chars12Declaring 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;13Reading 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();14PrintingThere are two methods you can use for printing:System.out.println(something);This prints something and ends the lineSystem.out.print(something);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.");15Program 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


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
Download Just Enough Java
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 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 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?