DOC PREVIEW
LETU COSC 2103 - Java Classes

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 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 20 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 20 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 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java ClassesSlide 2Slide 3Time ClassSlide 5Classes as ExtensionsSlide 7Running the Multi File ProgramObservationsSlide 10Creating a New Project with Visual J++ 6.0Creating A New ClassSource Code for the ClassCreating the Main ClassEntering Source Code for Main ClassSource Code for Main ClassBuild the ProgramRunning the ProgramSlide 19Properties Dialog BoxJava ClassesJava Classes•Consider this simplistic classpublic class ProjInfo{ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed via command line at a DOS prompt"); System.out.println("Syntax for command line is "); System.out.print("C:\\Jview Prog1 <integer>\n\n"); System.out.print("Example: C:\\PROG1 6\n\n"); System.out.print("Program computes, displays 720 which is 6!"); }}public class ProjInfo{ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed via command line at a DOS prompt"); System.out.println("Syntax for command line is "); System.out.print("C:\\Jview Prog1 <integer>\n\n"); System.out.print("Example: C:\\PROG1 6\n\n"); System.out.print("Program computes, displays 720 which is 6!"); }}Specified publicThis class has exactly one function, the constructorIt's task is to print information or directions for the userJava Classes•Calling the classimport ProjInfo;public class Class1{public ProjInfo intro;public static void main (String[] args){int count = 1, entered = 1, result = 1;ProjInfo intro = new ProjInfo();if (args.length ==1)count = entered = Integer.parseInt(args[0],10);else{ . . .import ProjInfo;public class Class1{public ProjInfo intro;public static void main (String[] args){int count = 1, entered = 1, result = 1;ProjInfo intro = new ProjInfo();if (args.length ==1)count = entered = Integer.parseInt(args[0],10);else{ . . .Class is importedA pointer to a class object is declared (but no class object is yet instantiated)• new is used to instantiate and initialize a class object• The constructor is called and executes the sequence of printline commandsTimeClass1// Fig. 8.1: Time1.java2 // Time1 class definition3 import java.text.DecimalFormat; // used for number formatting45 // This class maintains the time in 24-hour format66 public class Time1 extends Object {77 private int hour; // 0 - 238 private int minute; // 0 - 599 private int second; // 0 - 591011 // Time1 constructor initializes each instance variable12 // to zero. Ensures that each Time1 object starts in a 13 // consistent state.1414 public Time1()15 {16 setTime( 0, 0, 0 );17 }1819 // Set a new time value using universal time. Perform 20 // validity checks on the data. Set invalid values to zero.2121 public void setTime( int h, int m, int s )22 {23 hour = ( ( h >= 0 && h < 24 ) ? h : 0 );24 minute = ( ( m >= 0 && m < 60 ) ? m : 0 );25 second = ( ( s >= 0 && s < 60 ) ? s : 0 );26 }27private instance variables can only be accessed by methods in their class.Time1 constructor, initializes new Time1 objects.Each file needs exactly one public class, which is the filename. Time1 inherits from class Object.public method, may be accessed through a Time1 reference. Checks validity of arguments.Name of file and name of class must be same … including case of charactersTimeClass28 // Convert to String in universal-time format29 public String toUniversalString()30 {31 DecimalFormat twoDigits = new DecimalFormat( "00" );3233 return twoDigits.format( hour ) + ":" +34 twoDigits.format( minute ) + ":" +35 twoDigits.format( second );36 }3738 // Convert to String in standard-time format3939 public String toString()40 {41 DecimalFormat twoDigits = new DecimalFormat( "00" );42 43 return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +44 ":" + twoDigits.format( minute ) +45 ":" + twoDigits.format( second ) +46 ( hour < 12 ? " AM" : " PM" );47 }48 }Method toString implicitly knows to use instance variables of the object that invoked it.What other differences and similarities with C++ classes do you see?What other differences and similarities with C++ classes do you see?Classes as Extensionsclass Account {String name;String address;int accountNumber;void displayNameAndAddress() {System.out.println(name);System.out.println(address);}public static void main(String args[]) {Saving billsSavingAccount = new Saving();billsSavingAccount.balance = 99.22;billsSavingAccount.name = "Bill Simmons";billsSavingAccount.address = "37 Maple Ave, NN3 3TL";billsSavingAccount.accountNumber = 5672876;billsSavingAccount.displayAccountDetails(); } }class Account {String name;String address;int accountNumber;void displayNameAndAddress() {System.out.println(name);System.out.println(address);}public static void main(String args[]) {Saving billsSavingAccount = new Saving();billsSavingAccount.balance = 99.22;billsSavingAccount.name = "Bill Simmons";billsSavingAccount.address = "37 Maple Ave, NN3 3TL";billsSavingAccount.accountNumber = 5672876;billsSavingAccount.displayAccountDetails(); } }Given the class on the left:Given the class on the left:Where is this defined?Note the name of this classClasses as Extensionsclass Loan extends Account{int loanTerm;int numberOfRepayments;double loanSize;double balance;double monthlyRepayments;void displayAccountDetails() { System.out.println("This is a loan account Account number " + accountNumber); displayNameAndAddress(); System.out.println("Size of loan " + loanSize); System.out.println("Number of repayments " + numberOfRepayments); System.out.println("Outstanding balance " + balance); System.out.println("Monthly repayments " + monthlyRepayments);}}Data elementsPrint method“extends” specificationRunning the Multi File Program•Compile the “extends” class file first•Then compile the class with the public static void main (String args[ ])•Now run this fileObservationsclass Loan extends Account{int loanTerm;int numberOfRepayments;double loanSize;double balance;double monthlyRepayments;void displayAccountDetails ( ) { System.out.println("This is a loan account Account number " + accountNumber); displayNameAndAddress( ); System.out.println("Size of loan " + loanSize); System.out.println("Number of repayments " + numberOfRepayments);


View Full Document

LETU COSC 2103 - Java Classes

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Java Classes
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 Java Classes 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 Java Classes 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?