DOC PREVIEW
Penn CIT 591 - Sudden Java

This preview shows page 1-2-15-16-31-32 out of 32 pages.

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

Unformatted text preview:

Sudden JavaStructure of a Java programJava structure and EclipseSimple program outlineCommentsDeclaring variablesSome Java data typesReading in numbersPrintingProgram to double a numberAssignment statementsMethodsMethod types and returnsMethod callsOrganization of a classArithmetic expressionsBoolean expressionsString concatenationif statementsCompound statementswhile loopsThe do-while loopThe for loopParts of the for loopExample for loopsExample: Multiplication tableWhen do you use each loop?The assert statementEnabling assertionsA complete programAnother complete programThe EndJan 18, 2019Sudden Java2Structure of a Java programA program, or project, consists of one or more packages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• statementsProject:3Java 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Within the project, you next create a packageFinally, you create a class in that packageFor the simplest program, you need only a single package, and only one (or a very few) classes4Simple 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 methodCommentsPython: Single-line comments start with #Java: Single-line comments start with //Java: Multi-line comment start with /* and end with */Python: Documentation comments are enclosed in triple quotes, and are put right after the def lineJava: Documentation comments start with /** and end with */, and are put just before the definition of a variable, method, or classDocumentation comments are more heavily used in Java, and there are much better tools for working with them56Declaring variablesIn Python, a variable may hold a value of any typeIn Java, 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;7Some Java data typesIn Java, the most important primitive (simple) types are:int variables hold integer valuesdouble variables hold floating-point numbers (numbers containing a decimal point)boolean variables hold a true or false valueOther primitive types arechar variables hold single charactersfloat variables hold less accurate floating-point numbersbyte, short and long hold integers with fewer or more digitsAnother important type is the StringA String is an Object, not a primitive typeA String is composed of zero or more chars8Reading 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();9Printing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 values of any type with the + operatorExample:System.out.println("There are " + appleCount + " apples and " + orangeCount + " oranges.");10Program 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); }}11Assignment 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 forgotten12MethodsA method is a named group of declarations and statementsvoid tellWhatYearItIs( ) { int year = 2006; System.out.println("Hello in " + year + "!");}We “call,” or “invoke” a method by naming it in a statement:tellWhatYearItIs( );This should print out Hello in 2006!Method types and returnsEvery method definition must specify a return typevoid if nothing is to be returnedEvery method parameter must be typedExample: double average(int[] scores) { … }The return type is double, the parameter type is int[]If a method returns void (nothing), you may use plain return statements in itIf you reach the end of the method, it automatically returnsIf a method returns something other than void, you must supply return statements that specify the value to be returnedExample: return sum / count;1314Method callsA method call is a request to an object to do something, or to compute a valueSystem.out.print(expression) is a method call; you are asking the System.out object to evaluate and display the expressionWhen you call a method, do


View Full Document

Penn CIT 591 - Sudden 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 Sudden 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 Sudden 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 Sudden 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?