CS代写 SENG201

Software Engineering I SENG201

Lecture 4 – Introduction to Java February 28, 2022

Copyright By PowCoder代写 加微信 powcoder

Previous lecture
1. UML in a nutshell
2. Requirements and use cases
4. Testing
5. Operation and maintenance

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

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

Java versus Python
JDK 1.0 in January 1996 (end 1995)
Python 1.0 in January 1994
Declarations before use
No explicit declarations
Static typing
Dynamic typing
Interpreted
Family members include C#, C++
Family members include Tcl/TK, – everything is a class
Hybrid – mix of procedural and OO
• Java software development kit (JDK)
– Download (Java 17): https://jdk.java.net/17
– Download/setup: feel free to ask our friendly tutors for help if needed

https://blogs.oracle.com/javamagazine/the-top-25-greatest-java-apps-ever-written

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

Start with a UML class diagram
• For now only one class
• Structure of a single class in a UML class diagram
class name
properties (class variables)*
behavior (methods)
*Also often referred to as attributes, states, state variables, member variables, instance variables

Our first class – version 1
• What does the class know, what can the class do?
universalGreeting greetingText
greet() setGreeting()
Name of the class
What does the class know?
What can the class do?

Our first class – version 2
• Add implementation details (types, parameters, etc.)*
-universalGreeting: String = “Hello World!” -greetingText: String
+greet(): void +setGreeting(theGreeting:String): void
*Class diagrams (like all UML diagram types) are not limited to Java, but support object-oriented modelling in general

Are we ready to write Java code?

The code – Greeter.java
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();

The code – the class name
public class Greeter {
private String universalGreeting = “Hello World!”;
private String public void greet()
Name of the class
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();
niversalGreeting;

The code – properties
public class Greeter {
private String universalGreeting = “Hello World!”; private String greetingText = universalGreeting;
public void greet()
-universalGreeting: String = “Hello World!”
{ -greetin}gText: String
System.out.println(greetingText);
What does the class know
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();

The code – methods
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) {
What can the class do?
g.greet(); +setGreeting(theGreeting:String): void
g.setGreeting(“Hello New Zealand!”);
g.greet();
+greet(): void
Greeter g = new Greeter();

The code – the main 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();
Only declaration/implementation of method; method is not executed
Only declaration/implementation of method; methods is not executed
main* is “entry point”; each program needs at least one class with main method
Object is created Method is called; above is just declaration Method is called; above is just declaration Method is called; above is just declaration
*Every class can (but does not need to) have a main, e.g., for separate testing
Keep main small (process arguments, instantiate self, hand over to other methods)

Java’s virtual machine architecture
Java source code (.java) javac
Byte code1 (.class)
JVM2 JVM2 JVM2
1Byte code concept is used beyond Java (e.g., Common Intermediate Language CIL in .NET) 2JVM: Java Virtual Machine

Compile (javac)
C:\seng201>javac Greeter.java
C:\seng201>
javac .java translates Java source code into byte code (instructions for JVM)

Run (java)
C:\seng201>java Greeter
Hello World!
Hello New Zealand!
C:\seng201>
java interprets byte code and translates it into machine code for a particular platform
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();

Java packages and API
• Group classes written by other people or by ourselves – Similar to “libraries” or “modules” in other languages
Fast slide
– Allow us to reuse classes and methods
– Many classes and methods in the standard Java API (will be used a lot)
• For example, System.out.println() we reused in class Greeter • Java API

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

Comments in Java
• Document for you and for others
– Maintenance, new team members, reuse, etc.
• 3 types of comments
– Inline,e.g.,//Compilercan’tseeme.
– Multi-line,block,e.g.,
* I am a long comment.
* Compiler still can’t see me. */
– Javadoc,e.g., /**
Classes and interfaces only (include –author, i.e., javadoc –author …)
Classes and interfaces only (include –version, i.e., javadoc –version …)
For methods and constructors only
For methods only
* Can include HTML, tags
* Javadoc will process me. */

Javadoc – example
Fast slide
* This class implements an application that simply displays * a couple of greetings on the screen.
public class GreeterWithComments
* Prints a greeting to the screen.
public void greet(){…}
* Sets the greeting text.
* @param theGreeting the new text of the greeting *
public void setGreeting(String theGreeting){…}

Run javadoc
Fast slide
C:\seng201>javadoc GreeterWithComments.java
C:\seng201>
javadoc .java
creates documentation

Fast slide

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

Extension to illustrate object-orientation
public class Greeter {
private String universalGreeting = “Hello World!”; private String greetingText = universalGreeting;
public void greet(){…}
public void setGreeting(String theGreeting){…}
public static void main(String[] args) {
Greeter g1 = new Greeter();
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”);
g2.greet();

Let us zoom into main …
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”); g2.greet();
A new object
universalGreeting greetingText
Greeter g1 = new Greeter();
-universalGreeting: String = “Hello World!”
-greetingText: String
+greet(): void
+setGreeting(theGreeting:String): void

A new object has been created
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!” greetingText = “Hello World!”
Greeter g1 = new Greeter();
-universalGreeting: String = “Hello World!”
-greetingText: String
+greet(): void
+setGreeting(theGreeting:String): void
public class Greeter {
private String universalGreeting = “HelloWorld!”; private String greetingText = universalGreeting; …

Another object is created
Greeter g1 = new Greeter();
g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!” greetingText = “Hello World!”
Greeter g2 = new Greeter();
universalGreeting
greetingText
-universalGreeting: String = “Hello World!”
-greetingText: String
+greet(): void
+setGreeting(theGreeting:String): void
public class Greeter {
private String universalGreeting = “HelloWorld!”; private String greetingText = universalGreeting; …
Another new object

Another object has been created
Greeter g1 = new Greeter();
g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!” greetingText = “Hello World!”
Greeter g2 = new Greeter();
universalGreeting = “Hello World!”
greetingText = “Hello World!”
-universalGreeting: String = “Hello World!”
-greetingText: String
+greet(): void
+setGreeting(theGreeting:String): void
public class Greeter {
private String universalGreeting = “HelloWorld!”; private String greetingText = universalGreeting; …

Nothing happens to the objects
Greeter g1 = new Greeter(); Greeter g2 = new Greeter();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!” greetingText = “Hello World!”
g1.greet();
g2.greet();
universalGreeting = “Hello World!”
greetingText = “Hello World!”
public class Greeter {
public void greet() {
System.out.println(greetingText);

Now object g1 changes
Greeter g1 = new Greeter(); Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.greet(); g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!”
greetingText = “Hello New Zealand!”
g1.setGreeting(“Hello New Zealand!”);
universalGreeting = “Hello World!”
greetingText = “Hello World!”
public class Greeter {
public void setGreeting(String theGreeting) {
greetingText = theGreeting;

Nothing happens to the objects
Greeter g1 = new Greeter();
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”);
g2.setGreeting(“Hello Brazil!”); g2.greet();
universalGreeting = “Hello World!” greetingText = “Hello New Zealand!”
g1.greet();
universalGreeting = “Hello World!”
greetingText = “Hello World!”
public class Greeter {
public void greet() {
System.out.println(greetingText);

Now object g2 changes
Greeter g1 = new Greeter();
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.greet(); …
universalGreeting = “Hello World!” greetingText = “Hello New Zealand!”
g2.setGreeting(“Hello Brazil!”);
universalGreeting = “Hello World!”
greetingText = “Hello Brazil!”
public class Greeter {
public void setGreeting(String theGreeting) {
greetingText = theGreeting;

Nothing happens to the objects
Greeter g1 = new Greeter();
Greeter g2 = new Greeter(); g1.greet();
g2.greet();
g1.setGreeting(“Hello New Zealand!”); g1.greet();
g2.setGreeting(“Hello Brazil!”);
universalGreeting = “Hello World!” greetingText = “Hello New Zealand!”
g2.greet();
universalGreeting = “Hello World!”
greetingText = “Hello Brazil!”
public class Greeter {
public void greet() {
System.out.println(greetingText);

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

Cartoon of the day
Key lesson: Good code follows good design. If you think good design is expensive, you should look at the cost of bad design*.
*Inspired by ; cartoon: xkcd

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