Unformatted text preview:

COMP 401 OBJECTS Instructor Prasun Dewan PROGRAM COMPONENTS PHYSICAL OBJECTS Natural Objects Manufactured Objects Program Components 2 PROGRAM OBJECTS MANUFACTURED OBJECTS Factory Operations man ufact ured by perform accelerate brake Class insta nce of Program Object Methods execute invoke call Program Object Invoking new operation on class ordering new car from factory add subtract 3 CLASSIFICATION THROUGH FACTORIES man ufact ured by man ufact ured by 4 CLASSIFICATION THROUGH CLASSES ASquareCalculator inst a nc e of ASquareCalculator Instance ASquareCalculator Instance ABMICalculator inst anc e of ABMICalculator Instance ABMICalculator Instance 5 A SIMPLE INSTANTIATED CLASS No static because class will be instantiated Object Use public class ASquareCalculator public int square int x return x x No package Object Creation public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 6 PACKAGES 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 Class in different package must be imported using full name of class a la full file name 7 PACKAGES package math public class ASquareCalculator public int square int x return x x Class in same package need not be imported package math public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 8 PACKAGES package 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 needed 9 PACKAGES public class ASquareCalculator public int square int x return x x No package means package named default hence no import needed here public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 10 PACKAGES public 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 name 11 LONG NAME WITH NO IMPORT package math public class ASquareCalculator public int square int x return x x Can use the full name of class directly package main public class SquareCalculatorTester public static void main String args math ASquareCalculator squareCalculator new math ASquareCalculator System out println squareCalculator square 5 12 WHY IMPORTS FULL NAME package math public class ASquareCalculator public int square int x return x x package safemath public class ASquareCalculator public long square int x return x x Twice the size of ints package main import math ASquareCalculator Disambiguates public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 13 AMBIGUOUS IMPORT package math public class ASquareCalculator public int square int x return x x package safemath public class ASquareCalculator public long square int x return x x package main import math ASquareCalculator Ambiguous import safemath ASquareCalculator public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 14 WHY 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 hierarchical package math power public class ACubeCalculator 15 BROWSING JAVA CLASSES Very useful package 16 LANGUAGE VS LIBRARY Built in classes Do not have to be explicitly imported 17 CHANGING PARAMETER public class ASquareCalculator public int square int x return x x Calculates 5 5 public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 5 18 CHANGING PARAMETER public class ASquareCalculator public int square int x return x x Must change code public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator System out println squareCalculator square 341 19 RERUN PROGRAM How to not rerun program without writing tedious UI code public 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 Integer parseInt args 0 Must re run program 20 OBJECTEDITOR package math public class ASquareCalculator public int square int x return x x package main import math ASquareCalculator import bus uigen ObjectEditor public class SquareCalculatorTester public static void main String args ASquareCalculator squareCalculator new ASquareCalculator ObjectEditor edit squareCalculator ObjectEditor is predefined packaged class 21 EDITING ASQUARECALCULATOR INSTANCE 22 INVOKING A METHOD AND VIEWING THE RESULT 23 ANOTHER SIMPLE CLASS ABMICALCULATOR ASquareCalculator ABMICalculator Specification Given an integer x calculate the square of x Specification Given the weight kg and height m of a person calculate the person s body mass index a k a BMI package math public class ASquareCalculator public int square int x return x x 24 ABMICALCULATOR package bmi public class ABMICalculator public int calculateBMI int weight int height return weight height height Parameter and return types are integers But height m and weight kg are expressed as decimals How do we solve the discrepancy 25 ABMICALCULATOR double package bmi public class ABMICalculator public int calculateBMI int weight int height return weight height height Doubles


View Full Document

UNC-Chapel Hill COMP 401 - Objects

Documents in this Course
Recursion

Recursion

45 pages

Load more
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 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?