CSE144 Intermediate Programming for Industrial...

14
CSE144 Intermediate Programming for Industrial Engineering Lab Sessions Session 01 Res. Asst. M. Umut İZER

Transcript of CSE144 Intermediate Programming for Industrial...

Page 1: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

CSE144 Intermediate Programming for Industrial Engineering Lab Sessions Session 01

Res. Asst. M. Umut İZER

Page 2: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Printing Star Patterns import java.util.Scanner;

public class PS {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows needed to

print the pattern ");

int rows = scanner.nextInt();

System.out.println("## Printing the pattern ##");

for (int i=1; i<=rows; i++){

for (int j=1; j<=rows; j++){

System.out.print("*");

}

System.out.println();

}

scanner.close();

}

} 2

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 3: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Printing a-b Patterns import java.util.Scanner;

public class PS {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows needed to

print the pattern ");

int rows = scanner.nextInt();

System.out.println("## Printing the pattern ##");

for (int i=1; i<=rows; i++){

for (int j=1; j<=rows; j++) {

if(i==j) System.out.print("a");

else if(i!=j) System.out.print("b");

}

System.out.println();

}

scanner.close();

}

} 3

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 4: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Checking if a Number is Prime public class PS {

static void checkPrime(int n){

int i;

boolean flag=true;

if(n==0||n==1)

System.out.println(n+" is not prime number");

else{

for(i=2;i<=n/2;i++){

if(n%i==0){

System.out.println(n+" is not prime number");

flag=false;

break; } }

if(flag)

System.out.println(n+" is prime number");} }

public static void main(String args[]){

checkPrime(1);

checkPrime(3);

checkPrime(17);

checkPrime(9439);

checkPrime(433494437); }

}

4

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 5: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Putting Numbers in a Square import java.util.Scanner;

public class PS {

public static void main(String args[]){

Scanner sc=new Scanner(System.in);

int side;

System.out.println("Enter the side length of the square");

side=sc.nextInt();

int limit;

System.out.println("Enter the last number of sequence");

limit=sc.nextInt();

int arr[][]=new int[side][side];

int element=1;

for(int i=0;i<side;i++) {

for(int j=0;j<side;j++) {

if(element>limit) break;

arr[i][j]=element;

element=element+1;

System.out.print(arr[i][j]+" "); }

System.out.println(); }

}

} 5

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 6: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Immutable Objects in Java

• An immutable object is an object that will not change its internal state after creation.

• To create an immutable object you need to follow some simple rules:

• Don't add any setter method

• Declare all fields final and private

• If a field is a mutable object create defensive copies of it for getter methods

• If a mutable object passed to the constructor must be assigned to a field create a defensive copy of it

• Don't allow subclasses to override methods.

6

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 7: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Immutable Object import java.util.Date;

public class DateContainer {

private final Date date;

public DateContainer() {

this.date = new Date(); }

public Date getDate() {

return date; }

public static void main(String[] args){

DateContainer dateContainer = new DateContainer();

System.out.println(dateContainer.getDate());

dateContainer.getDate().setTime(dateContainer.getDate().getTime()

+ 1000);

System.out.println(dateContainer.getDate());

// Now dateContainer date is 1 second after

}

} 7

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 8: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Immutable Object import java.util.Date;

public class DateContainer {

private final Date date;

public DateContainer() {

this.date = new Date(); }

public Date getDate() {

return new Date(date.getTime()); }

public static void main(String[] args){

DateContainer dateContainer = new DateContainer();

System.out.println(dateContainer.getDate());

dateContainer.getDate().setTime(dateContainer.getDate().getTime()

+ 1000);

System.out.println(dateContainer.getDate());

// Now dateContainer date is not changed because we changed the

// copy, not the original date

}

} 8

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 9: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Immutable Object import java.util.Date;

public class DateContainer {

private final Date date;

public DateContainer(Date date) {

this.date = date; }

public Date getDate() {

return new Date(date.getTime()); }

public static void main(String[] args){

Date date = new Date();

DateContainer dateContainer = new DateContainer(date);

System.out.println(dateContainer.getDate());

date.setTime(date.getTime() + 1000);

System.out.println(dateContainer.getDate());

// Now dateContainer date is 1 second after also if the getter method

// create a defensive copy of date. We changed the reference passed to

// the constructor, not the copy.

}

}

9

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 10: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Immutable Object import java.util.Date;

public class DateContainer {

private final Date date;

public DateContainer(Date date) {

this.date = new Date(date.getTime()); }

public Date getDate() {

return new Date(date.getTime()); }

public static void main(String[] args){

Date date = new Date();

DateContainer dateContainer = new DateContainer(date);

System.out.println(dateContainer.getDate());

date.setTime(date.getTime() + 1000);

System.out.println(dateContainer.getDate());

// Now dateContainer date is not changed. We create a copy on the

// constructor so a change to the external date will not affect the

// internal state of DateContainer instance

}

}

10

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 11: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Immutable Objects in Java

• Note that if a field is a reference to an immutable object, it is not necessary to create defensive copies of it in the constructor and in the getter methods. It is enough to define the field as final and private.

• As an example of common immutable objects there are String, all primitive wrappers (Integer, Long, Double, Byte....), BigDecimal, BigInteger.

• If a subclass override a method it can return the original value of a mutable field instead of a defensive copy of it.

• To solve this problem it is possible to do one of the following:

• Declare the immutable class as final so it can't be extended

• Declare all methods of the immutable class final so they can't be overridden

• Create a private constructor and a factory to create instances of the immutable class because a class with private constructors can't be extended

11

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 12: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Immutable Class

• Immutable class means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable. We can create our own immutable class as well.

• Following are the requirements:

• The class must be declared as final (So that child classes can’t be created)

• Data members in the class must be declared as final (So that we can’t change the value of it after object creation)

• A parameterized constructor

• Getter method for all the variables in it

• No setters (To not have the option to change the value of the instance variable)

12

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 13: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

Example: Immutable Class public final class Student{

final String name;

final int regNo;

public Student(String name, int regNo){

this.name = name;

this.regNo = regNo; }

public String getName(){

return name; }

public int getRegNo(){

return regNo; }

}

class Test{

public static void main(String args[]){

Student s = new Student("ABC", 101);

System.out.println(s.getName());

System.out.println(s.getRegNo());

// Uncommenting below line causes error

// s.regNo = 102;

}

}

13

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1

Page 14: CSE144 Intermediate Programming for Industrial …mimoza.marmara.edu.tr/~umut.izer/PS/CSE144_PS1.pdfImmutable Objects in Java •An immutable object is an object that will not change

References

• https://javainterviewpoint.com/star-pattern-programs-in-java/

• https://www.javatpoint.com/prime-number-program-in-java

• https://dzone.com/articles/immutable-objects-in-java

• https://www.geeksforgeeks.org/create-immutable-class-java/

• https://www.javatpoint.com/this-keyword

• Liang, Y.D. (2011). PowerPoint Lecture Slides for Introduction to Java Programming, 8th Edition, Georgia Southern University.

14

27

.02

.20

20

C

SE1

44

Lab

Ses

sio

n 0

1