CSS161: Fundamentals of Computing

Slides:



Advertisements
Similar presentations
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Advertisements

Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Information Hiding and Encapsulation
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
SWE 510: Object Oriented Programming in Java1 Defining Classes 1 This slide set was compiled from the Absolute Java textbook and the instructor’s own class.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Mixing integer and floating point numbers in an arithmetic operation.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
By Mr. Muhammad Pervez Akhtar
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Crash course in the Java Programming Language
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
INPUT STATEMENTS GC 201.
CSS161: Fundamentals of Computing
CMSC 202 Static Methods.
Program Style Console Input and Output
CSS161: Fundamentals of Computing
Class Structure 16-Nov-18.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Starting JavaProgramming
Defining Classes and Methods
Exceptions & exception handling
CSS 161 Fundamentals of Computing Introduction to Computers & Java
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
A+ Computer Science INPUT.
MSIS 655 Advanced Business Applications Programming
Class Structure 7-Dec-18.
IFS410 Advanced Analysis and Design
Fundamentals 2.
Chapter 2: Java Fundamentals
Class Structure 2-Jan-19.
Expressions and Assignment Statements
Class Structure 25-Feb-19.
Classes and Objects Static Methods
Object Oriented Programming in java
CSS161: Fundamentals of Computing
Classes, Objects and Methods
Math class services (functions)
CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing
Programming with Arrays 1
Presentation transcript:

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

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

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

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 = 111.75; 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 = 117.75 + 37.95 + 15 156.0 = 88.50 +67.50 + 0 myCourse1(CSS161A) needs $164.7 myCourse2(CSS451A) needs $156.0 CSS161: Fundamentals of Computing

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

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

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

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

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

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

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p170’s exercises 1 ~ 2. CSS161: Fundamentals of Computing

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

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

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

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

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

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

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 4 1776 That is the 4th day of the month CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p176’s exercise 3 CSS161: Fundamentals of Computing