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 understandJava is currently a very popular languageJava is a large, powerful languagebut it is not simple!Compared to C++, Java is elegant3Structure of a Java programA simple program consists of a single packagePackage = directory = folderA package contains one or more classesA class contains one or more fields and methodsA method contains declarations and statementsClasses and methods may also contain commentsWe’ll begin by looking at the “insides” of methods• packages• classes• fields• methods• declarations• statements4Java structure and EclipseA workspace is where Eclipse keeps projectsWhen you use Eclipse to create a project (a single “program”), it creates a directory with that name in your workspaceWhen you use Eclipse to create a class, it creates a file in the project directory that has the same name as the classWhen 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 outlineNotes:The class name (MyClass) must begin with a capitalmain and run are methodsThis is the form we will use for nowOnce 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 semanticsSyntax is the “grammar” of the languageThe syntax of Java is large, but finiteSyntax must be absolutely correctThe computer will point out every syntax errorError messages may be helpful or misleadingSemantics is the “meaning” of your programSemantic errors cause your answers to be wrongYou may or may not get error messages7Two aspects of JavaJava has syntax and semanticsThis is where you beginIt is possible to learn everything about Java’s syntax and semanticsWe will cover most of Java’s syntax and semanticsJava comes with many built-in “packages”Packages are sort of like vocabulary bundlesTo be good at Java, you need to learn many packagesThere are more Java packages than you can ever learn8What you need to knowYou need to be able to:Read in data (for now, numbers)We will use a Scanner for thisSave numbers in variablesWe will use declarations to create variablesDo arithmetic on numbers to get new numbersWe will use assignment statementsTest whether or not to do somethingWe will use if statementsDo something repeatedlyWe will use while statementsPrint outputWe will use System.out.print and System.out.printlnNot absolutely necessary, but very helpful:We will use methods9Declarations, statements, commentsA declaration gives some information to the computer A statement tells the computer to do somethingStatements should really be called “commands”Comments are ignored by the computer—they are explanations of your program for human beings to readA method may contain declarations, statements, and comments10VariablesA variable is a “box” that holds dataEvery variable has a nameExamples: name, age, address, isMarriedVariables start with a lowercase letterIn multiword variables, each new word is capitalized Every variable has a type of value that it can holdFor example,name might be a variable that holds a String (sequence of characters)age might be a variable that holds an integer valueisMarried might be a variable that holds a boolean (true or false) value11Some Java data typesIn Java, the four most important primitive (simple) types are:int variables hold integer valuesdouble variables hold floating-point numbers, that is, numbers containing a decimal pointboolean variables hold a true or false valuechar variables hold single charactersAnother important type is the StringA String is an Object, not a primitive typeA String is composed of zero or more chars12Declaring variablesEvery variable that you use in a program must be declared (in a declaration)The declaration specifies the type of the variableThe declaration may give the variable an initial valueExamples:int age;int count = 0;double distance = 37.95;boolean isReadOnly = true;String greeting = "Welcome to CIT 591";String outputLine;13Reading in numbersFirst, 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 scannernew Scanner(...) says to make a new oneSystem.in says the scanner is to take input from the keyboardNext, 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();14PrintingThere are two methods you can use for printing:System.out.println(something);This prints something and ends the lineSystem.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 timeYou can concatenate things with the + operatorExample:System.out.println("There are " + appleCount + " apples and " + orangeCount + " oranges.");15Program to double a numberimport 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