CS代写 SENG201

Software Engineering I SENG201

Lecture 5 – Some Java details March 1, 2022

Copyright By PowCoder代写 加微信 powcoder

Previous lecture
1. Introduction
2. Our first class in Java
3. Documentation in Java
4. Extending our first Java class

1. Identifiers and data types 2. Declarations
3. Arithmetic operators
4. Strings in Java

Typical reoccurring issues for those new to Java

1. Identifiers and data types
2. Declarations
3. Arithmetic operators
4. Strings in Java

Identifiers
Name “things” (classes,
objects, properties, methods, etc.)

Remember last lecture
Any combination of letters, digits, ‘_’ and ‘$’, cannot begin with a digit
public class Greeter
private String universalGreeting = “Hello World!”; private String greetingText = universalGreeting;
public void greet()
System.out.println(greetingText); public void setGreeting(String theGreeting)
greetingText = theGreeting;
public static void main(String[] args)
Case sensitive (GreetingText names different thing)
Greeter g = new Greeter();
g.greet();
g.setGreeting(“Hello New Zealand!”);
g.greet();
Choose wisely (e.g., text1 is bad)
User-defined identifiers are in red

Conventions
• Class names are nouns and capitalized – E.g.,Greeter,FriendlyGreeter
• Method names are verbs/actions, methodName() – E.g.,go(),goHome()
• Variables are nouns (properties/states), variableName – E.g.,age,studentAge

Reserved words – cannot be identifiers
public class Greeter {
private String universalGreeting = “Hello World!”; private String greetingText = universalGreeting;
public void greet() {
System.out.println(greetingText);
public void setGreeting(String theGreeting) {
greetingText = theGreeting;
public static void main(String[] args) {
Greeter g = new Greeter(); g.greet();
g.setGreeting(“Hello New Zealand!”); g.greet();
… and many more
Reserved words are in red

Data types
• Data item (“variable”) has fixed type (strong type checking) Data types
Primitive data types*
• Whole numbers • byte
• int (e.g., int age = 19;) • long
• Fractional numbers
• double (e.g., double range = 1.5;)
• Characters
• char (e.g., char c = ‘d’;)
• True or false
• boolean (e.g., boolean status = true;)
Objects and classes
• Like variables, objects have “types” • Object is “instance” of class
• Createdwithnew(e.g.,
Greeter myGreeter = new Greeter();)
*Have different memory footprints: compiler allocates appropriate storage; for objects: depends on properties + methods

Data types matter – example
• Microsoft ® Exchange Server in January 2022
Fast slide
https://twitter.com/haydsays/status/1477147051209154562, https://twitter.com/aionescu/status/1477293671099867137 Full story: https://www.itnews.com.au/news/microsoft-kicks-off-2022-with-email-blocking-exchange-bug-574388

Wrapper classes
• Every primitive type has associated wrapper class
– Integer wrapper for int, Double wrapper for double, etc.
• Wrapper classes define
– Useful constants for maximum, minimum values of types – Conversionmethods(seeAPI)
System.out.println(Integer.MAX_VALUE); int i = Integer.parseInt(“17”);
Double dd = 42.65; // auto-boxing
double d = dd; // auto-unboxing – same as… double d = dd.doubleValue();

1. Identifiers and data types 2. Declarations
3. Arithmetic operators
4. Strings in Java

Declaring methods
public int bar(int i) {
System.out.println(12 + foo(42)); System.out.println(foo(4)); return i + 5;
• Expressions match parameters
• May be literals, arithmetic/Boolean expressions,
object references, method return values, etc.
• Involves
– Identifier
– Parameters
• Specify identifiers (≥0) and their type(s)
– Returntype(orvoid)
• Non-void methods end with return statement (matches declaration)
– Modifiers (visibility, static, etc.)

Declaration and initialization of variables
• Declare variables: give them names and types
• Initialize variables: either explicitly or relying on default values
• Default values
– Properties (instance variables) initialized to default values
• Zero for numeric types, false for boolean
• “null” for objects (any attempt to access null reference will crash)
– Local variables (e.g., inside methods) not initialized
• Need to be initialized explicitly
• Compiler won’t allow variables that are not initialized

public class VariableInitTester {
private int i;
private int z = 42; // z private int k, l;
private int m, n, o = 43; private int p, q = 3, r = 44; private int s = p = 45; private int s2 = s;
public void printInstanceVariables()
public void createAndPrintLocalVariables() {
int z = 6; // another z System.out.println(“Value of local a: ” +a); System.out.println(“Value of local z: ” + z);
public static void main(String[] args){…}
What is the ‘+’ in “+a”?

public class VariableInitTester {
private int i;
private int z = 42; // z private int k, l;
private int m, n, o = 43; private int p, q = 3, r = 44; private int s = p = 45; private int s2 = s;
public void printInstanceVariables() public void createAndPrintLocalVariables()
int a; // will not compile
int z = 6; // another z System.out.println(“Value of local a: ” +a); System.out.println(“Value of local z: ” + z);
public static void main(String[] args){…}

public class VariableInitTester {
private int i;
private int z = 42; // z private int k, l;
private int m, n, o = 43; private int p, q = 3, r = 44; private int s = p = 45; private int s2 = s;
public void printInstanceVariables() public void createAndPrintLocalVariables()
int z = 6; // another z //System.out.println(“Value of local a: ” +a); System.out.println(“Value of local z: ” + z);
public static void main(String[] args){…}

C:\seng201>java VariableInit of local z: 6
Value of instance variable i: 0 Value of instance variable z: 42 Value of instance variable k: 0 Value of instance variable l: 0 Value of instance variable m: 0 Value of instance variable n: 0 Value of instance variable o: 43 Value of instance variable p: 45 Value of instance variable q: 3 Value of instance variable r: 44 Value of instance variable s: 45 Value of instance variable s2: 45
C:\seng201>
public class VariableInitTester {
private int i;
private int z = 42;
private int k, l;
private int m, n, o = 43; private int p, q = 3, r = 44; private int s = p = 45; private int s2 = s;
public void printInstanceVariables() {
public void createAndPrintLocalVariables()
int z = 6; …

• Some properties represent “facts”
– Initially set to desired value and cannot be altered
– Java’s final modifier used to define constants (convention: CAPITAL)
public class ConstDemo {
public final int WEEKS_PER_YEAR = 52;
public final double PI = 3.14159;
public final double TWO_PI = PI + PI;
public final String VERSION = “Version 3.02b”; public final char STAR = ‘*’;
public final int i; // will not compile public double circleArea(double r)
return (PI * r * r);

Method overloading and signatures
• Class may have several methods with same identifier – Overloadedmethodsdistinguishedbysignature
– Signature:identifier+parameters
– Return type is not part of signature (cannot distinguish methods)
– Method println (overloaded in class java.lang.PrintStream)
public void println(){…}
public void println(int x){…} public void println(double x){…} public void println(String x){…} public void println(Object x){…}
// e.g., System.out.println();
// e.g., System.out.println(8);
// e.g., System.out.println(3.5);
// e.g., System.out.println(“Hello World!”); // e.g., System.out.println(student);

1. Identifiers and data types 2. Declarations
3. Arithmetic operators
4. Strings in Java

Arithmetic operators
Multiplication
Division (results type depends on arguments)
Modulo (remainder after integer division)
Subtraction
String concatenation
Assignment
• Increment (++, +=) and decrement (–, -=) – i++ versus++i
– j–versus–j – i+=5;i-=5;

public class ArithmeticOperatorExample {
public static void main(String[] argv) {
int i = 0; int j = 4;
System.out.println(i++);
System.out.println(++j);
i+=5; j-=5;
System.out.println(i);
System.out.println(j);

1. Identifiers and data types 2. Declarations
3. Arithmetic operators
4. Strings in Java

• Objects of String class, see API for methods – E.g.,length(),concat()
• String “literals” (double quotes)
– String name1 = new String(“Nicole”);
– String name2 = “Yuang”;
• What is the difference between the two?
• Empty string versus null reference – String s1 = “”;
– String s2 = null;

• Concatenate: + operator or concat() method
• Conversions: conversion methods in wrapper classes
String name = “Pedro” + “ ” + “Schulz”; String contact = name.concat(“Room 314”); System.out.println(“Extension: ” + 6347);
int i; String s;
i = 2 + Integer.parseInt(“2”); i = 2 + “2”;
s = “2” + Integer.toString(2); s = “2” + 2
// does not compile // s = “22”
// s = “22”
• Type of expressions determined by first seen
int i = 3; int j = 8;
System.out.println(i + j + “ = the sum”); // 11 = the sum System.out.println(“What is ” + i + j + “?”); // What is 38? System.out.println(“What is ” + (i + j) + “?”); // What is 11?

1. Identifiers and data types 2. Declarations
3. Arithmetic operators
4. Strings in Java

Cartoon of the day
Key lesson: Variables and data types matter. Types and names need to be chosen carefully.
https://xkcd.com/1306/

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com