Class Library, Formatting, Wrapper Classes, and JUnit Testing

Slides:



Advertisements
Similar presentations
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Advertisements

Java Software Solutions
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
1 A Sorting Example (a start for Lab 4 problem number 3)
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Static Class Members Wrapper Classes Autoboxing Unboxing.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Review and Java Questions 13 June Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
ELC 312 Day 4. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Agenda Questions? Problem set one due Problem set two Posted in WebCT  Due Sept.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Java Programming. Objects  Java is an object-oriented programming language –use objects to define both the data type and the operations that can be applied.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
1 CMPT 126 Java Basics Using Classes and Objects.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Variables in Java A variable holds either
Homework Assignments APP B Reference on Junit Testing.
Formatting Output & Enumerated Types & Wrapper Classes
Outline Creating Objects The String Class Packages Formatting Output
Using Classes and Objects
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Objects and Primitive Data
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Using java libraries CGS3416 spring 2019.
Presentation transcript:

Class Library, Formatting, Wrapper Classes, and JUnit Testing Java Class Library (Packages) Formatting Output Wrapper Classes and Autoboxing JUnit Testing Reading for this Lecture: L&L, 3.3 – 3.8

Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Its classes are not part of the Java language per se, but we rely on them heavily Various classes we've already used (System, Scanner, String) are part of the Java standard class library (Look them up on Sun website) Other class libraries can be obtained through third party vendors, or you can create them yourself

Packages The classes of the Java standard class library are organized into packages Some packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

The import Declaration When you want to use a class contained in a package, you can use its fully qualified name java.util.Scanner scan = ... Or you can import the package containing the class and just use the class name Scanner import java.util.Scanner; Scanner scan = ... To import all classes in a particular package, you can use the * wildcard character import java.util.*;

The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs The Scanner class, on the other hand, is part of the java.util package, so that class must be imported as part of its package

Formatting Output Look at NumberFormat and DecimalFormat classes in the text They provide you with ways to output numbers with a predefined precision For example: Printing double value of Pi 3.141592… Printing only 2 decimal digits 3.14

Leading Blanks for Numbers There is no Java library mechanism to put leading blanks on digit strings to achieve right hand alignment of column of numbers Need to write nested conditional code: System.out.println( "Number is: " + (n<10? " " + n : (n<100? " " + n : (n<1000? " " + n : n))));

Wrapper Classes The java.lang package contains a wrapper class that corresponds to each primitive type: Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void

Integer age = new Integer(40); Wrapper Classes The following declaration creates an Integer object which is a reference to an object with the integer value 40 Integer age = new Integer(40); An object of a wrapper class is used in situations where a primitive value will not suffice For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be

num = Integer.parseInt(str); Wrapper Classes Wrapper classes may contain static methods that help manage the associated type For example, the Integer class contains a method to convert digits stored in a String to an int value: num = Integer.parseInt(str); Wrapper classes often contain useful constants For example, the Integer class contains MIN_VALUE and MAX_VALUE for the smallest and largest int values

Autoboxing Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = num; The assignment creates the appropriate Integer object wrapping a value of 42 The reverse conversion (called unboxing) also occurs automatically as needed

JUnit Testing Testing is critical to software quality Good test plans are difficult to specify but also difficult to document precisely Good testing must be repeatable Good testing is tedious Testing is a good candidate for automation Some methodologies such as “Extreme Programming” mandate daily builds and automated unit testing

JUnit Testing In project 1, when we developed our Java code for the QuadraticSolver class, we used the CLI class itself as the “driver” to execute test cases We manually entered our test case values and visually verified whether the response provided was correct or not This testing process was labor intensive!! The JUnit framework helps us build a “test case” class to automate testing of a “class under test”

JUnit Testing “junit.framework.TestCase Class” TestSolver “Driver Class” TestCase TestSolver extends + assertEquals( ) + test2RealRoots( ) . . . depends on “Class Under Test” QuadraticSolver + setA( ) … + toString( ) + getSolution( )

JUnit Testing Useful method inherited from TestCase class: assertEquals(Object expected, Object actual) assertEquals(“expected”, cut.toString( )); The assertEquals method flags discrepancies between the “expected” value and the result returned by the “class under test” method( ) assertEquals method automatically displays the difference between the “expected value” and the actual return value received

JUnit Testing Other useful assert… methods assertEquals(double expected_value, double actual_value, double threshold_value) Automatically compares absolute difference between first two parameters with a threshold assertEquals(4.3, cut.getDbl(), 0.1);

JUnit Testing Useful assert… methods for boolean data type assertTrue(boolean actual_value) Automatically expects returned value is true assertTrue(cut.getBoolean()); assertFalse(boolean actual_value) Automatically expects returned value is false assertFalse(cut.getBoolean());

JUnit Test for QuadraticSolver import junit.framework.TestCase; public class TestSolver extends TestCase { private QuadraticSolver cut; public TestSolver() { // nothing needed here } // First of six test case methods for the QuadraticSolver class public void test2RealRoots() assertEquals("Solving: 1x\u00b2 + 0x -1 = 0", QuadraticSolver.getEquation(1, 0, -1)); assertEquals("Root 1 is 1.0\nRoot 2 is -1.0", QuadraticSolver.getSolution(1, 0, -1));

JUnit Testing Test Case Execution 1 test failed: TestSolver test2RealRoots test2ImaginaryRoots testOnly1Root testLinear testNoSolution testAnySolution File: C:\Documents and Settings\bobw\My Documents\bobw\public_html\CS110\Project1\JUnitSolution\TestSolver.java [line: 48] Failure: expected:<......> but was:<...1...> (I removed part of “should be” string constant to create error)

JUnit Testing The Java code in the TestCase class(es) precisely documents the test cases It allows them to be run automatically It allows people other than the test designer to run them without knowing the details It prevents oversights in identification of any discrepancies in the results