Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.

Similar presentations


Presentation on theme: "Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed."— Presentation transcript:

1 Java Classes

2 Consider this simplistic class 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 \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 \n\n"); System.out.print("Example: C:\\PROG1 6\n\n"); System.out.print("Program computes, displays 720 which is 6!"); } Specified public This class has exactly one function, the constructor It's task is to print information or directions for the user

3 Java Classes Calling the class 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 {... 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 commands

4 Time Class 1// Fig. 8.1: Time1.java 2// Time1 class definition 3import java.text.DecimalFormat; // used for number formatting 4 5// This class maintains the time in 24-hour format 6 6public class Time1 extends Object { 7 7 private int hour; // 0 - 23 8 private int minute; // 0 - 59 9 private int second; // 0 - 59 10 11 // Time1 constructor initializes each instance variable 12 // to zero. Ensures that each Time1 object starts in a 13 // consistent state. 14 14 public Time1() 15 { 16 setTime( 0, 0, 0 ); 17 } 18 19 // Set a new time value using universal time. Perform 20 // validity checks on the data. Set invalid values to zero. 21 21 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 } 27 private 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 characters

5 Time Class 28 // Convert to String in universal-time format 29 public String toUniversalString() 30 { 31 DecimalFormat twoDigits = new DecimalFormat( "00" ); 32 33 return twoDigits.format( hour ) + ":" + 34 twoDigits.format( minute ) + ":" + 35 twoDigits.format( second ); 36 } 37 38 // Convert to String in standard-time format 39 39 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?

6 Classes as Extensions 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(); } } 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: Where is this defined? Note the name of this class

7 Classes as Extensions class 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 elements Print method“extends” specification

8 Running 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 file

9 Observations class 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); } class 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); } Note: Data elements are NOT specified as private

10 Observations 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(); } } 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(); } } Data elements and display method can be accessed directly – this is really more of a struct Saving object instantiation looks like it’s declared as a pointer This is typical in Java – it is a pointer. They are automatically dereferenced

11 Creating a New Project with Visual J++ 6.0 Program opens with New Project window Choose Console Application Specify name for project Specify storage location Specify name for project Specify storage location

12 Creating A New Class Select Project, Add Item for dialog box Specify item to be a class Choose Class Specify item to be a class Choose Class Specify name of class Name it ProjInfo.java Specify name of class Name it ProjInfo.java Click on Open

13 Source Code for the Class Enter text for class in open window Don’t forget to save your work 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 \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 \n\n"); System.out.print("Example: C:\\PROG1 6\n\n"); System.out.print("Program computes, displays 720 which is 6!"); }

14 Creating the Main Class –Right click on the Class1.java in the Project Explorer window –Rename it to Prog1.java

15 Entering Source Code for Main Class Double click on Class1.java to bring up editing window Window will have default comments in it

16 Source Code for Main Class 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 { System.out.print("***Error***\n\n"); System.out.print("Please enter: C:\\Prog1 \n"); System.exit(0); } while (count > 1) { result *= count; count --; } System.out.println("The factorial of "+entered+" is "+result); } 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 { System.out.print("***Error***\n\n"); System.out.print("Please enter: C:\\Prog1 \n"); System.exit(0); } while (count > 1) { result *= count; count --; } System.out.println("The factorial of "+entered+" is "+result); }

17 Build the Program From Build menu, choose Build System will compile and link your program When you have a compiler error, double click in the message window –Editor will indicate source code line where error is detected

18 Running the Program From Debug menu, choose Start Program will run (with an error) –Window opens –Displays execution error as specified in code –Program designed to run from command line with an argument C:\JView Class1 6 Window also immediately closes

19 Running the Program Need to tell Visual Studio interactive environment to use a number as our argument From Project menu, select Properties for dialog box

20 Properties Dialog Box Run the program again (Debug and Start) –The DOS window still closes immediately Choose Custom radio button option Add the argument 6 to the command line call


Download ppt "Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed."

Similar presentations


Ads by Google