SlideShare a Scribd company logo
1 of 45
Download to read offline
Lecture 5
Wrappers and GUIs
Plan
▪ Wrappers
▪ GUIs
Wrappers
Wrapper Classes
▪ Wrapper classes are a way of using primitive data types
as objects.
▪ Each primitive data type will have a corresponding
wrapper class.
▪ The wrapper class gives us a way to convert primitive
data types into objects and convert objects into
primitive data types.
Wrapper Classes
Primitive DataType Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Integer i = 1;
Integer j = new Integer(5);
Integer k;
k = i + j;
System.out.println(k);
int i = 1;
int j = 5;
int k;
k = i + j;
System.out.println(k);
Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Boolean b = true;
if (b)
System.out.println("The
Boolean object is true.");
boolean b = true;
if (b)
System.out.println("The
boolean variable is
true.");
Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Double d;
d = ‐1.5;
d = Math.abs(d);
double d;
d = ‐1.5;
d = Math.abs(d);
Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Character c1 = 'A';
System.out.println(c1);
char c1 = 'A';
System.out.println(c1);
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– Constructors: Integer(int i), Integer(String s)
Integer j = new Integer(6); Integer j = new Integer("5");
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– String toString() – converts the integer to a string of the same number
String s = j.toString();
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– double doubleValue(), long longValue() – converts Integer to the
specified primitive type
double d = j.doubleValue();
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– Boolean equals(Integer) – returns true if this Integer is equal to the
passed Integer.
Integer i = new Integer(5);
Integer j = new Integer(6);
Boolean b = i.equals(j); //will be false
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: Class methods of the Integer class
– int max(int, int);
int i = 5; int k = 6;
int k = Integer.max(i,j);
Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: Class methods of the Integer class
– int parseInt(String);
String s = "4";
int l = Integer.parseInt(s);
Wrapper Classes
▪ The other classes, Float, Double, Boolean, have similar class
methods and object methods.
Wrapper Classes
▪ Example: two different ways of reading numbers from user:
Scanner scan = new Scanner(System.in);
int i;
System.out.println(“Enter a number: ");
i = scan.nextInt();
Returns an int
Wrapper Classes
▪ Example: two different ways of reading numbers from user:
Scanner scan = new Scanner(System.in);
int i;
System.out.println("Please enter a number: ");
i = Integer.parseInt(scan.next());
Returns a
String
Returns an
int
Wrapper Classes
▪ Example: two different ways of converting int to Integer :
▪ Explicitly - using the Integer.valueOf(i) class method
▪ Autoboxing – compiler implicitly uses the Integer.valueOf(i)
class method
int i = 5;
Integer k = Integer.valueOf(i);
int i = 5;
Integer j = i;
▪ Converts explicitly ▪ Autoboxing
Wrapper Classes
▪ Example: two different ways of converting Integer to int :
▪ Explicitly - using the intValue() object method
▪ Unboxing - compiler implicitly uses the intValue() object
method.
Integer i = new Integer(5);
int k = i.intValue();
Integer i = new Integer(5);
int j = i;
▪ Converts explicitly ▪ Unboxing
Wrapper Classes
Exercise: Write code that:
▪ creates an integer,
▪ then explicitly wraps it into an Integer,
▪ converts this Integer into a Double,
▪ then unwraps this Double to a double
Wrapper Classes
int i = 5;
Integer i2 = Integer.valueOf(i);
Double d = Double.valueOf(i2.doubleValue());
double d2 = d.doubleValue();
Wrapper Classes
Why do we want to go through all
this hassle of using Wrapper classes?
Java is always pass by value.
Wrapper Classes
▪ Recall that in C:
– & operator returns the address of the variable.
int main() {
int i = 4;
printf("%dn", i);
printf("%pn", &i);
system("PAUSE");
return 0;
}
4
00F7F8AC
Press any key to continue . . .
Wrapper Classes
▪ Recall that in C:
– Can also create a variable through its pointer.
int main() {
int *i;
i = malloc(sizeof(int));
*i = 5;
printf("%pn", i);
printf("%dn", *i);
system("PAUSE");
return 0;
}
012860B8
5
Press any key to continue . . .
Wrapper Classes
▪ However, when we pass objects by value, object
identifiers are references themselves.
▪ So it passes references by their values, which is
implicitly passing by reference.
Why do we want to go through all
this hassle of using Wrapper classes?
Java is always pass by value.
Wrapper Classes
▪ Therefore if we want to pass int, float, double
variables … by reference, we can’t…
▪ But we can create object wrappers (which are objects
that do the same thing as the primitive variables)…
▪ …therefore can pass by reference.
Primitive
Non-primitive
GUIS
GUIS
▪ So far, our programs have looked like this…
▪ We want to write programs that have a
more natural interface, rather than the
text-only interface of the console.
GUIS
▪ The elements that make up a window interface are objects.
– Frame objects
– Button objects
– TextField objects
– Label objects
– Menu objects
– Checkbox objects
– List objects
– Radio objects
GUIS
▪ These objects are part of the javax.swing package, so this
must be included.
▪ There are also bits and pieces that are from the java.awt
package.
GUIS
▪ Two main types of windows:
– General purpose frame – JFrame object
– Special purpose frame – JDialog object
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Create a JFrame object.
▪ This object has methods that can be used to set its title, its
size and visibility:
JFrame jF = new JFrame();
jF.setTitle("GUIs are awesome!");
jF.setSize(400,300);
jF.setVisible(true);
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Also a method to set its position (relative to the top left
corner)
jF.setLocation(200,250);
200
250
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ We make sure that our program ends when the window is
closed using the setDefaultCloseOperation() method
▪ It is passed a class constant: JFrame.EXIT_ON_CLOSE
jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Class constant of
the JFrame class
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ The main part of the window is called the content pane – it
contains everything but the title, menu bars and border.
▪ In order to do stuff, e.g. add buttons to it, we need it as
object.
▪ But we are not going to create a new object, we are going
to get the object that is already there:
Container CP = jF.getContentPane();
Container is a class, part of the
java.awt package – ignore this for now
*
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Now that we have the Container object, we can e.g. change its
color:
▪ Color is a class in java.awt package and Color.BLUE is a class
constant.
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ One of the main uses of constants is that they give a
significant label that can be used, without having to worry
about what it is
▪ Color is a class in java.awt package used to represent
colours. It has class constants to represent common colours,
like Color.BLUE
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ We can combine these two lines:
▪ Use the returned content pane directly
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
jF.getContentPane()
Returns the content pane
Returns the content pane
.setBackground(Color.BLUE);
Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ Creates special purpose frames
▪ showMessageDialog() – simple message.
▪ First argument specifies where it is centred.
JOptionPane.showMessageDialog(jF, "Java is so much fun!");
JOptionPane.showMessageDialog(null, "Java is so much fun!");
Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ Creates special purpose frames
▪ showInputDialog() – simple message with box for user
to enter text.
▪ First argument specifies where it is centred.
String s = JOptionPane.showInputDialog(jF, “Enter your age");
Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ showInputDialog() always returns a String.
▪ If we want numerical data from the user, we must
convert it – using wrapper classes
String s = JOptionPane.showInputDialog(jF, “Enter your age?");
int i = Integer.parseInt(s);
Simple GUI I/O with JOptionPane

More Related Content

Similar to Lecture 5.pdf

Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptxKavitaHegde4
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdfKavitaHegde4
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
c#(loops,arrays)
c#(loops,arrays)c#(loops,arrays)
c#(loops,arrays)sdrhr
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
Object Oriented Programming_combined.ppt.pdf
Object Oriented Programming_combined.ppt.pdfObject Oriented Programming_combined.ppt.pdf
Object Oriented Programming_combined.ppt.pdfAshutoshKumar211882
 
Shiksharth com java_topics
Shiksharth com java_topicsShiksharth com java_topics
Shiksharth com java_topicsRajesh Verma
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Mohamed Nabil, MSc.
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Tiểu Hổ
 
Programming in Java: Storing Data
Programming in Java: Storing DataProgramming in Java: Storing Data
Programming in Java: Storing DataMartin Chapman
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 

Similar to Lecture 5.pdf (20)

Java Generics
Java GenericsJava Generics
Java Generics
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
c#(loops,arrays)
c#(loops,arrays)c#(loops,arrays)
c#(loops,arrays)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
Object Oriented Programming_combined.ppt.pdf
Object Oriented Programming_combined.ppt.pdfObject Oriented Programming_combined.ppt.pdf
Object Oriented Programming_combined.ppt.pdf
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
Shiksharth com java_topics
Shiksharth com java_topicsShiksharth com java_topics
Shiksharth com java_topics
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
Kotlin
KotlinKotlin
Kotlin
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
Programming in Java: Storing Data
Programming in Java: Storing DataProgramming in Java: Storing Data
Programming in Java: Storing Data
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 

More from SakhilejasonMsibi (9)

Lecture 6.pdf
Lecture 6.pdfLecture 6.pdf
Lecture 6.pdf
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
 
Lecture 11.pdf
Lecture 11.pdfLecture 11.pdf
Lecture 11.pdf
 
Lecture2.pdf
Lecture2.pdfLecture2.pdf
Lecture2.pdf
 
Lecture 8.pdf
Lecture 8.pdfLecture 8.pdf
Lecture 8.pdf
 
Lecture 9.pdf
Lecture 9.pdfLecture 9.pdf
Lecture 9.pdf
 
Lecture 4 part 1.pdf
Lecture 4 part 1.pdfLecture 4 part 1.pdf
Lecture 4 part 1.pdf
 
Lecture 10.pdf
Lecture 10.pdfLecture 10.pdf
Lecture 10.pdf
 
Lecture1.pdf
Lecture1.pdfLecture1.pdf
Lecture1.pdf
 

Recently uploaded

NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024EMMANUELLEFRANCEHELI
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological universityMohd Saifudeen
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfmahaffeycheryld
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfssuser5c9d4b1
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxMustafa Ahmed
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisDr.Costas Sachpazis
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
The Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptxThe Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptxMANASINANDKISHORDEOR
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfJNTUA
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docxrahulmanepalli02
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AISheetal Jain
 
Low Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s HandbookLow Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s HandbookPeterJack13
 
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Nitin Sonavane
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdfKamal Acharya
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Studentskannan348865
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksIJECEIAES
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesRashidFaridChishti
 

Recently uploaded (20)

NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdf
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
The Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptxThe Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptx
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Low Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s HandbookLow Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s Handbook
 
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 

Lecture 5.pdf

  • 4. Wrapper Classes ▪ Wrapper classes are a way of using primitive data types as objects. ▪ Each primitive data type will have a corresponding wrapper class. ▪ The wrapper class gives us a way to convert primitive data types into objects and convert objects into primitive data types.
  • 5. Wrapper Classes Primitive DataType Wrapper Class byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
  • 6. Wrapper Classes ▪ Usage - we can use the wrapper classes in the same way that we can the primitive versions. Integer i = 1; Integer j = new Integer(5); Integer k; k = i + j; System.out.println(k); int i = 1; int j = 5; int k; k = i + j; System.out.println(k);
  • 7. Wrapper Classes ▪ Usage - we can use the wrapper classes in the same way that we can the primitive versions. Boolean b = true; if (b) System.out.println("The Boolean object is true."); boolean b = true; if (b) System.out.println("The boolean variable is true.");
  • 8. Wrapper Classes ▪ Usage - we can use the wrapper classes in the same way that we can the primitive versions. Double d; d = ‐1.5; d = Math.abs(d); double d; d = ‐1.5; d = Math.abs(d);
  • 9. Wrapper Classes ▪ Usage - we can use the wrapper classes in the same way that we can the primitive versions. Character c1 = 'A'; System.out.println(c1); char c1 = 'A'; System.out.println(c1);
  • 10. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: These are some objects methods of the Integer class – Constructors: Integer(int i), Integer(String s) Integer j = new Integer(6); Integer j = new Integer("5");
  • 11. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: These are some objects methods of the Integer class – String toString() – converts the integer to a string of the same number String s = j.toString();
  • 12. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: These are some objects methods of the Integer class – double doubleValue(), long longValue() – converts Integer to the specified primitive type double d = j.doubleValue();
  • 13. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: These are some objects methods of the Integer class – Boolean equals(Integer) – returns true if this Integer is equal to the passed Integer. Integer i = new Integer(5); Integer j = new Integer(6); Boolean b = i.equals(j); //will be false
  • 14. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: Class methods of the Integer class – int max(int, int); int i = 5; int k = 6; int k = Integer.max(i,j);
  • 15. Wrapper Classes ▪ Because wrappers are objects, they have methods ▪ Example: Class methods of the Integer class – int parseInt(String); String s = "4"; int l = Integer.parseInt(s);
  • 16. Wrapper Classes ▪ The other classes, Float, Double, Boolean, have similar class methods and object methods.
  • 17. Wrapper Classes ▪ Example: two different ways of reading numbers from user: Scanner scan = new Scanner(System.in); int i; System.out.println(“Enter a number: "); i = scan.nextInt(); Returns an int
  • 18. Wrapper Classes ▪ Example: two different ways of reading numbers from user: Scanner scan = new Scanner(System.in); int i; System.out.println("Please enter a number: "); i = Integer.parseInt(scan.next()); Returns a String Returns an int
  • 19. Wrapper Classes ▪ Example: two different ways of converting int to Integer : ▪ Explicitly - using the Integer.valueOf(i) class method ▪ Autoboxing – compiler implicitly uses the Integer.valueOf(i) class method int i = 5; Integer k = Integer.valueOf(i); int i = 5; Integer j = i; ▪ Converts explicitly ▪ Autoboxing
  • 20. Wrapper Classes ▪ Example: two different ways of converting Integer to int : ▪ Explicitly - using the intValue() object method ▪ Unboxing - compiler implicitly uses the intValue() object method. Integer i = new Integer(5); int k = i.intValue(); Integer i = new Integer(5); int j = i; ▪ Converts explicitly ▪ Unboxing
  • 21. Wrapper Classes Exercise: Write code that: ▪ creates an integer, ▪ then explicitly wraps it into an Integer, ▪ converts this Integer into a Double, ▪ then unwraps this Double to a double
  • 22. Wrapper Classes int i = 5; Integer i2 = Integer.valueOf(i); Double d = Double.valueOf(i2.doubleValue()); double d2 = d.doubleValue();
  • 23. Wrapper Classes Why do we want to go through all this hassle of using Wrapper classes? Java is always pass by value.
  • 24. Wrapper Classes ▪ Recall that in C: – & operator returns the address of the variable. int main() { int i = 4; printf("%dn", i); printf("%pn", &i); system("PAUSE"); return 0; } 4 00F7F8AC Press any key to continue . . .
  • 25. Wrapper Classes ▪ Recall that in C: – Can also create a variable through its pointer. int main() { int *i; i = malloc(sizeof(int)); *i = 5; printf("%pn", i); printf("%dn", *i); system("PAUSE"); return 0; } 012860B8 5 Press any key to continue . . .
  • 26. Wrapper Classes ▪ However, when we pass objects by value, object identifiers are references themselves. ▪ So it passes references by their values, which is implicitly passing by reference. Why do we want to go through all this hassle of using Wrapper classes? Java is always pass by value.
  • 27. Wrapper Classes ▪ Therefore if we want to pass int, float, double variables … by reference, we can’t… ▪ But we can create object wrappers (which are objects that do the same thing as the primitive variables)… ▪ …therefore can pass by reference. Primitive Non-primitive
  • 28. GUIS
  • 29. GUIS ▪ So far, our programs have looked like this… ▪ We want to write programs that have a more natural interface, rather than the text-only interface of the console.
  • 30. GUIS ▪ The elements that make up a window interface are objects. – Frame objects – Button objects – TextField objects – Label objects – Menu objects – Checkbox objects – List objects – Radio objects
  • 31. GUIS ▪ These objects are part of the javax.swing package, so this must be included. ▪ There are also bits and pieces that are from the java.awt package.
  • 32. GUIS ▪ Two main types of windows: – General purpose frame – JFrame object – Special purpose frame – JDialog object
  • 33. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ Create a JFrame object. ▪ This object has methods that can be used to set its title, its size and visibility: JFrame jF = new JFrame(); jF.setTitle("GUIs are awesome!"); jF.setSize(400,300); jF.setVisible(true);
  • 34. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ Also a method to set its position (relative to the top left corner) jF.setLocation(200,250); 200 250
  • 35. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ We make sure that our program ends when the window is closed using the setDefaultCloseOperation() method ▪ It is passed a class constant: JFrame.EXIT_ON_CLOSE jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Class constant of the JFrame class
  • 36. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ The main part of the window is called the content pane – it contains everything but the title, menu bars and border. ▪ In order to do stuff, e.g. add buttons to it, we need it as object. ▪ But we are not going to create a new object, we are going to get the object that is already there: Container CP = jF.getContentPane(); Container is a class, part of the java.awt package – ignore this for now *
  • 37. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ Now that we have the Container object, we can e.g. change its color: ▪ Color is a class in java.awt package and Color.BLUE is a class constant. Container CP = jF.getContentPane(); CP.setBackground(Color.BLUE);
  • 38. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ One of the main uses of constants is that they give a significant label that can be used, without having to worry about what it is ▪ Color is a class in java.awt package used to represent colours. It has class constants to represent common colours, like Color.BLUE Container CP = jF.getContentPane(); CP.setBackground(Color.BLUE);
  • 39. Simple GUI I/O with JOptionPane JFrame class – JFrame creates a simple window. ▪ We can combine these two lines: ▪ Use the returned content pane directly Container CP = jF.getContentPane(); CP.setBackground(Color.BLUE); jF.getContentPane() Returns the content pane Returns the content pane .setBackground(Color.BLUE);
  • 40. Simple GUI I/O with JOptionPane JOptionPane – allows for simply GUI-based input and output ▪ Creates special purpose frames ▪ showMessageDialog() – simple message. ▪ First argument specifies where it is centred. JOptionPane.showMessageDialog(jF, "Java is so much fun!"); JOptionPane.showMessageDialog(null, "Java is so much fun!");
  • 41.
  • 42.
  • 43. Simple GUI I/O with JOptionPane JOptionPane – allows for simply GUI-based input and output ▪ Creates special purpose frames ▪ showInputDialog() – simple message with box for user to enter text. ▪ First argument specifies where it is centred. String s = JOptionPane.showInputDialog(jF, “Enter your age");
  • 44. Simple GUI I/O with JOptionPane JOptionPane – allows for simply GUI-based input and output ▪ showInputDialog() always returns a String. ▪ If we want numerical data from the user, we must convert it – using wrapper classes String s = JOptionPane.showInputDialog(jF, “Enter your age?"); int i = Integer.parseInt(s);
  • 45. Simple GUI I/O with JOptionPane