Sudden Java Jan 18 2019 Structure of a Java program A program or project consists of one or more packages 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 Project packages classes fields methods declarations statements 2 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 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 classes 3 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 4 Comments 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 them 5 Declaring 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 6 Some Java data types In Java the most important primitive simple types are Other primitive 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 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 chars 7 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 8 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 values of any type with the operator Example System out println There are appleCount apples and orangeCount oranges 9 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 10 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 11 Methods 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 12 Method types and returns Every method definition must specify a return type 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 void if nothing is to be returned 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 13 Method calls A method call is a request to an object to do something or to compute a value When you call a method do not specify parameter You must provide parameters of the type specified in the method definition A method call may be used as a statement System out print expression is a method call you are asking the System out object to evaluate and display the expression Example System out print 2 pi radius Some method calls return a value and those may be used as part of an expression Example h Math sqrt a a b b 14 Organization of a class A class may contain data declarations and methods and constructors which are like methods but not statements A method may contain temporary data declarations and statements A common error class Example int variable simple declaration is OK int anotherVariable 5 declaration with initialization is OK variable 5 statement This is a syntax error void someMethod int yetAnotherVariable declaration is OK yetAnotherVariable 5 statement inside method is OK 15 Arithmetic expressions Arithmetic expressions may contain An operation involving two ints results in an int to indicate addition to indicate subtraction to indicate multiplication to indicate division to indicate remainder of a division integers only parentheses to indicate the order in which to do things When dividing one int by another the fractional part of the result is thrown away 14 5 gives 2 Any
View Full Document
Unlocking...