DOC PREVIEW
UNC-Chapel Hill COMP 401 - Objects

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Program Components ~ Physical ObjectsProgram Objects ~ Manufactured ObjectsClassification Through FactoriesClassification Through ClassesA Simple Instantiated ClassPackagesPackagesPackagesPackagesPackagesLong name with no importWhy imports/full name?Ambiguous ImportWhy Packages?Browsing Java ClassesLanguage vs. LibraryChanging ParameterChanging ParameterRerun ProgramObjectEditorEditing ASquareCalculator InstanceInvoking a Method And Viewing the ResultAnother Simple Class: ABMICalculatorABMICalculatorABMICalculatorABMICalculatorFormal vs. Actual ParametersProgrammed CallProgrammed vs. interactive callInternal vs. External Method CallsErrorsMethod Access ErrorExtra slidesProgram Objects ~ Manufactured ObjectsProgram Objects ~ Manufactured ObjectsCOMP 401OBJECTSInstructor: Prasun Dewan2PROGRAM COMPONENTS ~ PHYSICAL OBJECTSNatural ObjectsManufactured Objects~ Program Components~ Program Components3PROGRAM OBJECTS ~ MANUFACTURED OBJECTS manufactured byperform OperationsaccelerateacceleratebrakebrakeClassProgram ObjectProgram ObjectProgram ObjectProgram Objectinstance ofMethodsaddaddsubtractsubtractexecute invoke callFactoryInvoking new operation on class ~ ordering new car from factory4CLASSIFICATION THROUGH FACTORIESmanufacturedbymanufacturedby5CLASSIFICATION THROUGH CLASSESASquareCalculatorABMICalculatorASquareCalculator InstanceASquareCalculator InstanceASquareCalculator InstanceASquareCalculator InstanceABMICalculator InstanceABMICalculator InstanceABMICalculator InstanceABMICalculator Instanceinstance ofinstance of6A SIMPLE INSTANTIATED CLASSpublic class ASquareCalculator {public int square(int x) {return x*x;}}public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Object CreationObject CreationObject UseObject UseNo static because class will be instantiatedNo static because class will be instantiatedNo packageNo package7PACKAGESpackage math; public class ASquareCalculator {public int square(int x) {return x*x;}}package main; import math.ASquareCalculator;public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Class in different package must be imported using full name of class ( a la full file name)Class in different package must be imported using full name of class ( a la full file name)8PACKAGESpackage math; public class ASquareCalculator {public int square(int x) {return x*x;}}package math; public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Class in same package need not be importedClass in same package need not be imported9PACKAGESpackage math; public class ASquareCalculator {public int square(int x) {return x*x;}}import math.ASquareCalculator;public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}No package means package named default, hence import neededNo package means package named default, hence import needed10PACKAGESpublic class ASquareCalculator {public int square(int x) {return x*x;}}public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}No package means package named default, hence no import needed hereNo package means package named default, hence no import needed here11PACKAGESpublic class ASquareCalculator {public int square(int x) {return x*x;}}package main;public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Short name of class in default package same as its full nameShort name of class in default package same as its full name12LONG NAME WITH NO IMPORTpackage math; public class ASquareCalculator {public int square(int x) {return x*x;}}package main;public class SquareCalculatorTester{public static void main (String[] args) { math.ASquareCalculator squareCalculator = new math.ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Can use the full name of class directlyCan use the full name of class directly13package safemath; public class ASquareCalculator {public long square(int x) {return x*x;}}WHY IMPORTS/FULL NAME?package math; public class ASquareCalculator {public int square(int x) {return x*x;}}package main; import math.ASquareCalculator;public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Twice the size of intsTwice the size of intsDisambiguatesDisambiguates14package safemath; public class ASquareCalculator {public long square(int x) {return x*x;}}AMBIGUOUS IMPORTpackage math; public class ASquareCalculator {public int square(int x) {return x*x;}}package main; import math.ASquareCalculator;import safemath.ASquareCalculator;public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}AmbiguousAmbiguous15WHY PACKAGES?Can create competing implementations of same class.A la creating files Test.java in different assignment directories/foldersCan browse related classesA la browsing through all files in an assignment directory/folder.Like directories/folders packages can be hierarchicalpackage math.power;public class ACubeCalculator {…}16BROWSING JAVA CLASSESVery useful packageVery useful package17LANGUAGE VS. LIBRARYBuilt-in classesBuilt-in classesDo not have to be explicitly importedDo not have to be explicitly imported18CHANGING PARAMETERpublic class ASquareCalculator {public int square(int x) {return x*x;}}public class SquareCalculatorTester{public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator();System.out.println (squareCalculator.square(5)); }}Calculates 5*5Calculates 5*519CHANGING PARAMETERpublic class ASquareCalculator {public int square(int x) {return x*x;}}public class


View Full Document

UNC-Chapel Hill COMP 401 - Objects

Documents in this Course
Recursion

Recursion

45 pages

Load more
Download Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?