Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 1 Slides # SSUI Mobile Lab (Spring 2020) ## Week 1: Java Refresher .title-slide-logo[ ![Android Logo](java/android-logo.png) ] --- # Quick Java Refresher --- ## What is Java? -- - Strongly, statically typed language - Every variable has a type - This type is decided at compile time (mostly) -- - Compiled, class-based, Object-oriented -- - Platform agnostic - __Write once__, _run anywhere_ without recompilation - Especially useful for Android --- ## Java Basics: Primitive Types -- - Boolean ```java boolean hasClassStarted = true; boolean isClassOver = false; ``` -- - Integer ```java int numStudents = rand.nextInt(30); ``` -- - Float ```java float gradePointAverage = 3.2f; ``` -- - Double - Higher precision than float ```java double examScore = 97.362; ``` -- - Byte, Short, etc. --- ## Java Basics: Text -- - Characters ```java char section = 'B'; ``` -- - Strings ```java String instructor = "Jennifer Mankoff"; ``` -- All non-primitives types inherit from `Object` class - Including `String`; note the capitalization --- ## Java Basics: Visibility Modifiers ```java public final String COURSE = "CSE 340"; ///... private final String SSN = "123-45-6789"; ``` -- - `private` - Kept secret, can only be read/written by `self` - Cannot be accessed by subclasses -- - `package private` - This is the default access if no modifier is specified - Accessible by all classes in the same package. -- - `protected` - Access restricted to `self`, subclasses, and package -- - `public` - The world can read/write (fields) or call (methods) --- ## Java Basics: Visibility Modifiers -- - Generally, you want to be as restrictive as possible - Usually, this means `private` -- - Create getter/setter methods to modify the member variables -- - .red[__Almost never use__ `public`] for fields - Except for constants --- ## Java Basics: `final` -- - Prevent value from changing after initialization ```java final double courseGrade = 95.0; // local variable cannot be modified ever! ``` -- - Prevent subclassing ```java public final class Person { // can't subclass (for example to make a Student class) // ... } ``` -- - Prevent overriding ```java public final int getValue() { // can't override! return 0; } ``` --- ## Java Basics: `static` -- - Use for constants or variables are shared by all instances of a particular class ```java final static double SALES_TAX_RATE = 0.07; // Class Constant (never changes) static double mTotalAmount = 3.56; // Class variable can change ``` -- - Methods that can be called without an class instance (instantiating an object) ```java static String toString(int i); // For example Integer.toString(100) => "100"; ``` --- # Naming Conventions - class names are PascalCased - local variables and method names are camelCased - class or instance variables begin with a 'm' (for member), such as mTotalAmount - constants are UPPER_SNAKE_CASED --- ## Java Basics: Methods - Methods in Java typically follow this format: ```java {visibility} [static] [final] returnType methodName(paramType paramName, ...) { // ... } ``` -- - `static` and `final` are optional, special modifiers - `visibility` is one of `public`, `private`, `protected`, or empty for package private --- ## Java Basics: Method Example Summing two numbers and returning the answer as a string ```java public String getSumOfTwoNumbersAsString(int first, int second) { int sum = first + second; return Integer.toString(sum); // could also return "" + sum } ``` --- ## Java Basics: Declaring a class ```Java {visibility} class ClassName { // Field declarations // Method definitions } ``` --- ## Java Basics: Constructing a Class ```java public class Student { // Class (static) variables - public static final String STUDENT_KEY = "STUDENT"; private static final String ID_PREFIX = "S"; // Instance Variables private String mIdNumber; private String mName; // Constructors - used to create an instance Student(String name, String idNumber) { this.name = mName; this.idNumber = mIdNumber; } // Methods public String getPrefixedIdNumber() { return ID_PREFIX + mIdNumber; } ``` --- ## Java Basics: Constructing a Class cont. ```java // Getter public String getName() { return mName; } // Setter public void setName(String newName) { if (newName == null || newName == "") { newName = "Unknown"; } mName = newName; } // ... etc. } ``` --- # Enums An enum type is a special data type that enables for a variable to be a set of predefined constant ```java public enum EssentialGeometry { INSIDE, OUTSIDE }; ... EssentialGeometry where = EssentialGeometry.INSIDE; ``` --- # Switch Statements A form of a conditional with different execution paths ```java public enum EssentialGeometry { INSIDE, ON_EDGE, OUTSIDE }; ... EssentialGeometry where = EssentialGeometry.INSIDE; switch (where) { case ON_EDGE: // do the edgy things break; case INSIDE: // do the inside things but also fall through // and do the OUTSIDE things because no break statement; case OUTSIDE: // do the outside things break; default: // do default things // automatically falls through } ``` --- ## More Java Resources - Java Documentation (https://docs.oracle.com/en/java/javase/13/docs/api/) - __Online Java Practice Problems__: - http://codingbat.com/java - https://practiceit.cs.washington.edu/problem/list 3/30/20 © Lauren Bricker, University of Washington