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

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
How to Create a Java program CS115 Fall George Koutsogiannakis.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 7 Improving the User Interface
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Microsoft Visual Basic 2005: Reloaded Second Edition
Lesson 7: Improving the User Interface
1 INF160 IS Development Environments AUBG, COS dept Lecture 06 Title: Dev Env: Code::Blocks (Extract from Syllabus) Reference:
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Excel Tutorial 8 Developing an Excel Application
Eclipse.
Chapter 2: The Visual Studio .NET Development Environment
3 Introduction to Classes and Objects.
BİL527 – Bilgisayar Programlama I
Value-Returning Functions
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
CSC 113 Tutorial QUIZ I.
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
CIS16 Application Development Programming with Visual Basic
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Packages From Deitel & Deitel.
Lab 1 Introduction to C++.
Constructors, GUI’s(Using Swing) and ActionListner
CS360 Client/Server Programming Using Java
Java Programming with BlueJ Objectives
Introduction to Object-Oriented Programming
Java Looking at our first console application in Eclipse
Presentation transcript:

Java Classes

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

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

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; // private int minute; // private int second; // // Time1 constructor initializes each instance variable 12 // to zero. Ensures that each Time1 object starts in a 13 // consistent state public Time1() 15 { 16 setTime( 0, 0, 0 ); 17 } // Set a new time value using universal time. Perform 20 // validity checks on the data. Set invalid values to zero 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

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

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 = ; 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 = ; billsSavingAccount.displayAccountDetails(); } } Given the class on the left: Where is this defined? Note the name of this class

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

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

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

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 = ; 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 = ; 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

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

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

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!"); }

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

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

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); }

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

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

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

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