SSE3052: Embedded Systems Practice

Similar documents
COE318 Lecture Notes Week 10 (Nov 7, 2011)

Programming II (CS300)

INTERFACE WHY INTERFACE

CS1150 Principles of Computer Science Objects and Classes

Inheritance and Polymorphism

Programming II (CS300)

Index COPYRIGHTED MATERIAL

Introduction to Programming Using Java (98-388)

Abstract class & Interface

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CIS 110: Introduction to Computer Programming

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

CH. 2 OBJECT-ORIENTED PROGRAMMING

Create a Java project named week9

CS Week 13. Jim Williams, PhD

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

CompuScholar, Inc. 9th - 12th grades

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

Using Java reflection to reduce Code and Development time in DFS

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Programming II (CS300)

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

REVIEW EXERCISES. Draw an inheritance diagram that shows the relationships between these classes.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

TeenCoder : Java Programming (ISBN )

Java Object Oriented Design. CSC207 Fall 2014

Programming II (CS300)

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Programming overview

WEEK 13 EXAMPLES: POLYMORPHISM

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CS 113 PRACTICE FINAL

CIS 110: Introduction to computer programming

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CS 200 More Classes Jim Williams, PhD

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Exception-Handling Overview

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Inheritance and Polymorphism

Distributed Systems Recitation 1. Tamim Jabban

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CMSC 132: Object-Oriented Programming II. Inheritance

Answer ALL Questions. Each Question carries ONE Mark.

Lab 14 & 15: String Handling

Review. these are the instance variables. these are parameters to the methods

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

UNIVERSITI SAINS MALAYSIA. CIT502 Object-Oriented Programming and Software Engineering

Java Fundamentals (II)

AP CS Unit 6: Inheritance Exercises

Object-Oriented Concepts

CS 201, Fall 2016 Sep 28th Exam 1

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Week 5-1: ADT Design

Distributed Systems Recitation 1. Tamim Jabban

Inheritance Introduction. 9.1 Introduction 361

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Informatik II (D-ITET) Tutorial 6

C# Programming for Developers Course Labs Contents

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Software and Programming 1

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CS-202 Introduction to Object Oriented Programming

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Chapter 14 Abstract Classes and Interfaces

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Chapter 5 Object-Oriented Programming

Template. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Questions Answer Key Questions Answer Key Questions Answer Key

Use the scantron sheet to enter the answer to questions (pages 1-6)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Operators and Expressions

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

AP CS Unit 8: Inheritance Exercises

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Name: CS 159 Practice Final Fall 2015

Transcription:

SSE3052: Embedded Systems Practice Minwoo Ahn minwoo.ahn@csl.skku.edu Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)

Agenda Learning Java! Installation Class, method, and object Inheritance Predefined class (String) Interface Exception handling ArrayList SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 2

Object oriented programming Vehicle class Inheritance Color Type Fuel Company https://kienthuc24h.com/wp-content/uploads/2017/07/class.jpg SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 3

Installation JDK (Java Development Kit) Check if JDK is installed (maybe not installed) $javac version If not, install JDK $sudo apt install default-jdk Eclipse IDE Go to http://www.eclipse.org/downloads/ Download Eclipse IDE for Java Developers Untar Execute eclipse-inst SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 4

Warm up (Hello World!) public class HelloWorld { public static void main(string[] args) { System.out.println( Hello World! ); HelloWorld class main method (~= function in C language) public access modifier (public, private, protected) SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 5

C code vs. Java code C code int funca() { int funcb() { int funcc() { int main() { funca(); funcb(); funcc(); Java code class classa { int methoda() { int methodb() { int methodc() { class classb { int methoda() { int methodb() { int methodc() { class classc { static void main() { SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 6

Class A class is a template that describes data and behavior Similar with structure in C language Ex) class person { String firstname; // Instance variable public String getfirstname() { return firstname; //Getter (method) public void setfirstname(string firstname) { this.firstname = firstname; // Setter (method) SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 7

class Person { String firstname; String lastname; int age; public void Person(String a, String b, int value) { firstname = a; lastname = b; age = value; Constructor public String getfirstname() { return firstname; public void setfirstname(string firstname) { this.firstname = firstname; public String getlastname() { return lastname; public void setlastname(string lastname) { this.lastname = lastname; public int getage() { return age; public void setage(int age) { this.age = age; public String tostring() { return firstname + " " + lastname; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 8

Object An object is an instance of a class Ex) public class Main { public static void main(string[] args) { Person person = new Person( Jim, Knopf, 21); Person p2 = new Person( Jill, Sanders, 20); // Jill gets married to Jim // and takes his name p2.setlastname( Knopf ); System.out.println(p2); // Jill Knopf will printed SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 9

Constructor A special method that is invoked when creation of instance Name of constructor is ALWAYS the name of the class No return type One class can include several constructor (overloading) If no explicit constructor is defined, compiler implicitly adds a constructor with default values SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 10

Example (3 constructors) class Person { String firstname; String lastname; int age; public Person() { firstname = "No"; lastname = "Name"; age = 0; public Person(String a, String b) { firstname = a; lastname = b; age = 0; public Person(String a, String b, int value) { firstname = a; lastname = b; age = value; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 11

Method overloading More than on method with the same name, different arguments Type of return value doesn t matter Ex) Class PrintStream { public void println(string s) { public void println(double a) { (O) class PrintStream { public void println(string s) { public int println(string s) { (X) SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 12

Exercise 1 Implement a class circle that has methods double getarea() and double getperimeter(). In the constructor, supply the radius of the circle. If radius is not given by user, default value is 10. SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 13

Inheritance A subclass which derived from superclass Superclass Person Ex) Class Person { Subclass Student Doctor Professor Class Students extends Person { int grade; public Student() { SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 14

Example (Student derived from Person) class Student extends Person { int grade; public Student(String a, String b, int age, int grade) { super(a, b, age); //Constructor of superclass Person this.grade = grade; public int getgrade() { return grade; public void setgrade(int grade) { this.grade = grade; public String tostring() { return getfirstname() + " " + getlastname() + " is grade " + grade; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 15

Example cont. (Main) class Main { public static void main(string[] args) { Student s1 = new Student("Bill", "Gates", 13, 9); s1.setfirstname("steve"); s1.setgrade(10); System.out.println(s1); // Steve Gates is grade 10 will printed SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 16

Method overriding Method redeclaration Ex) class BaseClass { public String tostring() { return Base ; class ChildClass extends BaseClass { public String tostring() { return Child ; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 17

Method overriding public class Main { public static void main(string[] args){ BaseClass a = new BaseClass(); ChildClass b = new ChildClass(); System.out.println(a); // base System.out.println(b); // child SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 18

Available access modifier Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N No modifier Y Y N N private Y N N N SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 19

String class String class represents array of characters All string literals are implemented as instances of String class Strings are immutable Initialization Ex) String s = text ; Ex) String s = new String( text ); SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 20

String methods char charat(int index) Returns the char value at the specified index. String concat(string str) Concatenates the specified string to be the end of this string. boolean endswith(string suffix) Tests if this string ends with the specified suffix. boolean equals(object anobject) Compares this string to the specified object. int indexof(int ch) Returns the index within this string of the first occurrence of the specified character. int indexof(string str) Returns the index within this string of the first occurrence of the specified substring. int length() Returns the length of this string. More APIs: https://docs.oracle.com/javase/8/docs/api/ SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 21

Exercise 2 Write a superclass Worker and subclasses HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a method computepay(int hours) that computes the weekly pay for every worker. An hourly worker gets paid the hourly wage for the actual number of hours worked, if hours is at most 40. If the hourly worker worked more than 40 hours, the excess is paid at time and a half. The salaried worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours is. Supply a test program to test these classes and methods. (Provide tostring() for each classes) SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 22

Interface Declares a set of methods and their signatures Unlike class Provides no implementation Cannot instantiate Does not contain any constructor Ex) public interface Worker { double computepay(int hours); SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 23

implement for interface public class HourlyWorker implements Worker { private double rate; public HourlyWorker(double rate) { this.rate = rate; public double computepay(int hours) { if (hours > 40) return (hours 40) * rate * 1.5 + 40 * rate; else return hours * rate; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 24

implement for interface cont. public class SalariedWorker implements Worker { private int rate; public SalariedWorker(int rate) { this.rate = rate; public double computepay(int hours) { if (hours > 40) return 40 * rate; else return hours * rate; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 25

implement for interface cont. public class Main { public static void main(string[] args) { Worker worker1 = new Worker(); // Error Worker worker2 = new HourlyWorker(7.5); Worker worker3 = new SalariedWorker(10); System.out.println("worker2: " + worker2.computepay(40)); System.out.println("worker3: " + worker3.computepay(80)); SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 26

implement for interface cont. public class SomeClass { public void howmuch(worker worker) { System.out.println("You earn " + worker2.computepay(40)); // You earn 300 will printed for worker2 // You earn 400 will printed for worker3 SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 27

Implementing multiple interfaces A class can implement multiple interfaces Cannot inherit multiple superclasses Ex) public interface AAA { public int a(); public interface BBB { public int b(); public class CCC implements AAA,BBB { public int a() { ; public int b() { ; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 28

Exception handling An exception is an event to indicate an error Catch Throw SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 29

Types of exceptions Checked exception Checked at compile-time All subclasses of Exception except RuntimeException Unchecked Exception Not checked at compile-time rather checked at runtime Subclasses of RuntimeException Ex) ArithmeticException Int a = 50 / 0; Ex) NullPotinerException String s = null; System.out.println(s.length); Ex) ArrayIndexOutofBoundsException Int a[] = new int [5]; a[10] = 50; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 30

Throwing exception public class BankAccount { public void withdraw(double amount) { if (amount > balance) { // // now what? balance = balance amount; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 31

Throwing exception cont. public class BankAccount { public void withdraw(double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgum entexception("amount exceeds balance"); throw exception; balance = balance amount; SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 32

Catching exception public static void main(string[] args) { BankAccount acct = new BankAccount(100); try { acct.withdraw(200); catch (IllegalArgumentException ex) { System.out.println("Withdraw failed."); //or ex.printstacktrace(); SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 33

Exercise 3 Modify the BankAccount class to throw IllegalArgumentException when the account is constructed with a negative balance, when a negative amount is deposited, or when an amount that is not between 0 and the current balance is withdrawn. Write a test program that causes all three exceptions to occur and that catches them all SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 34

ArrayList An ArrayList is a sequence of objects Ex) Assume that Coin class is already defined ArrayList coins = new ArrayList(); coins.add(new Coin(0.1, dime )); coins.add(new Coin(0.25, quarter )); SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 35

ArrayList methods boolean add(e e) Appends the specified element to the end of this list. void add(int index, E element) Inserts the specified element at the specified position in this list. E get(int index) Returns the element at the specified position in this list. E remove(int index) Removes the element at the specified position in this list. int size() Returns the number of elements in this list. SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 36

Exercise 4 Implement a class Bank that contains an array list of BankAccount objects. Support methods, public void addaccount(double initialbalance) public void deposit(int account, double amount) public void withdraw(int account, double amount) public double getbalance(int account) An account number is simply an index into the array list SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 37