Download presentation
Presentation is loading. Please wait.
1
CSS161: Fundamentals of Computing
Defining Classes - Day 1 This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
2
CSS161: Fundamentals of Computing
Class and Objects nextInt( ) next( ) nextLine( ) … keyboard object main( ) Scanner.class Scanner keyboard = new Scanner( System.in ); Scanner inFile = null; try { Scanner inFile = new Scanner( new FileInputStream( “data.txt” ) ); } catch ( FileNotFoundException e ) { System.exit( 0 ); } int data1 = keyboard.nextInt( ); String message = keyboard.nextLine( ); Int data2 = inFile.nextInt( ); String message = inFile.nextLine( ); instantiated nextInt( ) { do some action } next( ) nextLine( ) … nextInt( ) next( ) nextLine( ) … inFile object instantiated 123 how are you? 98765 yet alive! CSS161: Fundamentals of Computing
3
Primitive Data Types versus Classes
a single piece of data byte, char, short, int, long, float, double, boolean Variables declared before their use: int a = 10, b = 20; Classes a collection of multiple pieces of data + methods Objects instantiated before their use Including the same methods Including the same set of data, but Maintaining different values in each piece of data MyClass object1 = new MyClass( ); MyClass object2 = new MyClass( ); object1.data = 10; object2.data = 20; 10 a 20 b 10 data method( ) object1 20 data method( ) object2 CSS161: Fundamentals of Computing
4
CSS161: Fundamentals of Computing
Class Example public class CourseDemo { public static void main( String[] args ) { Course myCourse1 = new Course( ); Course myCourse2 = new Course( ); myCourse1.department = "CSS"; myCourse1.number = 161; myCourse1.section = 'A'; myCourse1.textPrice1 = ; myCourse1.textPrice2 = 37.95; myCourse1.courseFee = 15; myCourse2.department = "CSS"; myCourse2.number = 451; myCourse2.section = 'A'; myCourse2.textPrice1 = 88.50; myCourse2.textPrice2 = 67.50; myCourse2.courseFee = 0; System.out.println( "myCourse1(" + myCourse1.department + myCourse1.number + myCourse1.section + ") needs $" + myCourse1.totalExpenditure( ) ); System.out.println( "myCourse2(" + myCourse2.department + myCourse2.number + myCourse2.section + ") needs $" + myCourse2.totalExpenditure( ) ); } public class Course { public String department; // CSS, IAS, BUS, NRS, EDU public int number; // course number public char section; // A, B, C ... public double textPrice1; // primary textbook public double textPrice2; // secondary textbook public double courseFee; // laboratory fee public double totalExpenditure( ) { return textPrice1 + textPrice2 + courseFee; } Multiple pieces of data Method instantiated instantiated myCourse2 myCourse1 department: CSS number: 451 section: A textPrice1: $88.50 textPrice2: $67.50 courseFee: $0 totalExpenditure( ) department: CSS number: 161 section: A textPrice1: $111.75 textPrice2: $37.95 courseFee: $15 totalExpenditure( ) 164.7 = 156.0 = myCourse1(CSS161A) needs $164.7 myCourse2(CSS451A) needs $156.0 CSS161: Fundamentals of Computing
5
Members, Instance Variables, and Methods
Class name public class Course { public String department; // CSS, IAS, BUS, NRS, EDU public int number; // course number public char section; // A, B, C ... public int enrollment; // the current enrollment public int limit; // the max number of students public double textPrice1; // primary textbook public double textPrice2; // secondary textbook public double courseFee; // laboratory fee public int availableSpace( ) { return limit - enrollment; } public double capacity( ) { return ( double )enrollment / limit; public double totalExpenditure( ) { return textPrice1 + textPrice2 + courseFee; Instance variables: (data members) Each object has its own values in these variables. members Methods: Each object has the same methods (actions, computations). CSS161: Fundamentals of Computing
6
Object Instantiation with new
Declare a reference variable (a box that contains a reference to a new instance.) ClassName object; object has no reference yet, (= null). Create a new instance from a given class. object = new ClassName( ); object has a reference to a new instance. Declare a reference variable and initialize it with a reference to a new instance created from a given class ClassName object = new ClassName( ); CSS161: Fundamentals of Computing
7
Accessing Instance Variables
Declaring an instance variable in a class public type instanceVariable; // accessible from any methods (main( )) private type instanceVariable; // accessible from methods in the same class Examples: public String department; public int number; public char section; public int enrollment; Assigning a value to an instance variable of a given object objectName.instanceVariable = expression; myCourse1.department = "CSS"; myCourse1.number = 161; myCourse1.section = 'A'; myCourse1.enrollment = 24; Reading the value of an instance of a given object Operator objectName.instanceVariable operator Example: System.out.println( "myCourse1 = " + myCourse1.department + myCourse1.number + ")" ); CSS161: Fundamentals of Computing
8
Defining and Accessing Methods
Defining a method in a class public type method( ) { // heading // method body //code to perform some action and/or compute a value } Example public double totalExpenditure( ) { return textPrice1 + textPrice2 + courseFee; Accessing a method in a class objectName.method( ); System.out.println( "myCourse1(" + myCourse1.department + myCourse1.number + myCourse1.section + ") needs $" + myCourse1.totalExpenditure( ) ); Public: accessible from any other methods (including main( )) Private: accessible from a method within the same class If a method takes only a certain action, this should be void and “return” is unnecessary. CSS161: Fundamentals of Computing
9
CSS161: Fundamentals of Computing
Class Names and Files Each class should be coded in a separate file whose name is the same as the class name + .java postfix. Example Source code Course.java CourseDemo.java Compilation (from DOS/Linux command line.) javac Course.java javac CourseDemo.java javac CourseDemo.java javac Course.java Compiled code Course.class CourseDemo.class Execution (from DOS/Linux command line.) java CourseDemo An either way is fine. Compiling CourseDemo.java first automatically compile Course.java, too. Start with the class name that includes main( ). CSS161: Fundamentals of Computing
10
CSS161: Fundamentals of Computing
Textbook Example - 4.1 public class DateFirstTry { public String month; public int day; public int year; //a four digit number. public void writeOutput( ) System.out.println(month + " " + day + ", " + year); } public class DateFirstTryDemo { public static void main(String[] args) DateFirstTry date1, date2; date1 = new DateFirstTry( ); date2 = new DateFirstTry( ); date1.month = "December"; date1.day = 31; date1.year = 2007; System.out.println("date1:"); date1.writeOutput( ); date2.month = "July"; date2.day = 4; date2.year = 1776; System.out.println("date2:"); date2.writeOutput( ); } date1: December 31, 2007 date2: July 4, 1776 CSS161: Fundamentals of Computing
11
CSS161: Fundamentals of Computing
Self-Test Exercises Work on textbook p170’s exercises 1 ~ 2. CSS161: Fundamentals of Computing
12
CSS161: Fundamentals of Computing
More About Methods Two kinds of methods Methods that only perform an action public void methodName( ) { /* body */ } Example public void writeOutput( ) { System.out.println(month + " " + day + ", " + year); } Methods that compute and return a result pubic type methodName( ) { /* body */ return a_value_of_type; public double totalExpenditure( ) { return textPrice1 + textPrice2 + courseFee; Nothing to return The same data type A variable, a constant, an expression, or an object CSS161: Fundamentals of Computing
13
CSS161: Fundamentals of Computing
return Statement Method to perform an action void Method( ) Return is not necessary but may be added to end the method before all its code is ended Example public void writeMesssage( ) { System.out.println( “status” ); if ( ) { System.out.println( “nothing” ); return; } else if ( error == true ) System.out.print( “ab” ); System.out.println( “normal” ); Method to perform an action type Method( ) Return is necessary to return a value to a calling method. Example public double totalExpenditure( ) { return textPrice1 + textPrice2 + courseFee; } CSS161: Fundamentals of Computing
14
Any Method Can Be Used As a void Method
A method that returns a value can also perform an action If you want the action performed, but do not need the returned value, you can invoke the method as if it were a void method, and the returned value will be discarded: objectName.returnedValueMethod(); CSS161: Fundamentals of Computing
15
CSS161: Fundamentals of Computing
public and private Public Methods and instance variables accessible from outside of their class Private Methods and instance variables accessible within their class public class Course css263.method1( ) public type method1( ) { } css263.method2( ) public type method2( ) { } css263.utility( ) private type utility( ) { } css263.department public String department; public int number; private int enrollment; private int gradeAverage; css263.number css263.enrollment CSS161: Fundamentals of Computing
16
Encapsulating Data in Class
Which design is more secured from malicious attacks? public class Course public class Course public type method1( ) { } public type method1( ) { } css161.method1( ) css263.method1( ) public type method2( ) { } public type method2( ) { } css161.utility( ) public type utility( ) { } private type utility( ) { } css161.number = 263 public String department; public int number; public int enrollment; public int gradeAverage; private String department; private int number; private int enrollment; private int gradeAverage; CSS161: Fundamentals of Computing
17
CSS161: Fundamentals of Computing
Textbook Example - 4.2 public int getMonth( ) { if (month.equalsIgnoreCase("January")) return 1; else if (month.equalsIgnoreCase("February")) return 2; else if (month.equalsIgnoreCase("March")) return 3; else if (month.equalsIgnoreCase("April")) return 4; else if (month.equalsIgnoreCase("May")) return 5; else if (month.equals("June")) return 6; else if (month.equalsIgnoreCase("July")) return 7; else if (month.equalsIgnoreCase("August")) return 8; else if (month.equalsIgnoreCase("September")) return 9; else if (month.equalsIgnoreCase("October")) return 10; else if (month.equalsIgnoreCase("November")) return 11; else if (month.equalsIgnoreCase("December")) return 12; else System.out.println("Fatal Error"); System.exit(0); return 0; //Needed to keep the compiler happy } import java.util.Scanner; public class DateSecondTry { private String month; private int day; private int year; //a four digit number. public void writeOutput( ) System.out.println(month + " " + day + ", " + year); } public void readInput( ) Scanner keyboard = new Scanner(System.in); System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); month = keyboard.next( ); day = keyboard.nextInt( ); year = keyboard.nextInt( ); public int getDay( ) return day; public int getYear( ) return year; CSS161: Fundamentals of Computing
18
CSS161: Fundamentals of Computing
Textbook Example - 4.3 public class DemoOfDateSecondTry { public static void main(String[] args) DateSecondTry date = new DateSecondTry( ); date.readInput( ); int dayNumber = date.getDay( ); System.out.println("That is the " + dayNumber + "th day of the month."); } Enter month, day, and year. Do not use a comma. July That is the 4th day of the month CSS161: Fundamentals of Computing
19
CSS161: Fundamentals of Computing
Self-Test Exercises Work on textbook p176’s exercise 3 CSS161: Fundamentals of Computing
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.