COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Using Classes to Store Data Computer Science 2 Gerb.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
1 COMP 110 Inheritance Tabitha Peck M.S. April 16, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
COMP 110 More Loops and Strings Tabitha Peck M.S. February 18, 2008 MWF 3-3:50 pm Philips
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Designing Programs Tabitha Peck M.S. January 16, 2008 MWF 3-3:50 pm Philips
COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
Hello AP Computer Science!. What are some of the things that you have used computers for?
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CSC 212 Object-Oriented Programming and Java Part 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Day 5. Task: Implement your vending machine code into the program with JOptionPane I/O (modify it) it to me.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
Computer Science I Lab 1 ISMAIL ABUMUHFOUZ | CS 180.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS 240 – Computer Programming I Lab
CSC111 Quick Revision.
Passing Objects to Methods
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
COMPUTER 2430 Object Oriented Programming and Data Structures I
using System; namespace Demo01 { class Program
Yanal Alahmad Java Workshop Yanal Alahmad
A first Look at Classes.
CS305J Introduction to Computing
Something about Java Introduction to Problem Solving and Programming 1.
Computing Adjusted Quiz Total Score
Michele Weigle - COMP 14 - Spr 04
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
class PrintOnetoTen { public static void main(String args[]) {
Announcements Assignment 2 and Lab 4 due Wednesday.
Methods/Functions.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Lab 4 Due Friday Office Hours - Today after Class Help with loops Help with Lab 4 Jason Jerald 2

Participants Needed for Virtual Reality Motion Perception in Virtual Environments During Head Turns What you get Learn about programming in a research environment Learn what grad students are doing in computer science Help contribute to improving virtual reality technology Earn real money The task select which of two presentations you believe contains scene motion trials Earn $.05 for each correct selection - Do the math! Study takes 3-5 Hours For more info Filling out form is not committing Name Preferred contact ( or phone) Preferred hours 3

Questions? 4

Today in COMP 110 Worksheet Review (quick) Debugger Classes 5

Debugger

What is the length? thisisastring

Classes Class Object Methods 8

Classes Class Object Methods 9

Classes Class Object Methods 10

UML Diagram 11 Class Name: Student Class Year GPA Major Credits GPA sum + getMajor + printData + increaseYear How: increase year by 1 + calcGPA How: average grades

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 12

Class File Each class in SEPARATE file Create instance of Class in main You will have TWO (or more) files!! Call methods in main You must run your file from main!!!!!!! 13

Create instance of Class in main public class StudentStats { public static void main(String[] args) { // instantiating object of type student Student s1 = new Student(); } Class ObjectClass Different Name From Class 14

Create Class in Separate File public class Student { //everything in your class goes here } Save as Student.java 15

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 16

Data (in your class file) 17 public int classYear; private double gpa; public String major; private int credits = 0; private double gpaSum; private - only access variables INSIDE your class public - access to variables OUTSIDE your class

Public Variables Student s1 = new Student(); s1.classYear = 2; classYear is a variable NOT a method Don’t use () 18

Private Variables Variables you do not want a user to manipulate GPA - calculate GPA in your class Don’t want user to reset the value 19

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 20

Method getMajor /*getMajor Input: none Return: major*/ public String getMajor() { return major; } 21 Return type Type String

Calling method getMajor 22 public static void main(String[] args) { Student s1 = new Student(); s1.major = “English”; String m = s1.getMajor(); System.out.print(m); }

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 23

Method increaseYear /*increaseYear Input: none Description: increase student year by one Return: none*/ public void increaseYear() { if (classYear < 4) classYear++; } 24

Calling method increaseYear 25 public static void main(String[] args) { Student s1 = new Student(); s1.classYear = 2; s1.increaseYear(); // classYear = 3 s1.increaseYear(); // classYear = 4 }

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 26

Method calcGpa /*calcGpa Input: grade(double) Description: update credits and gpaSum calculate GPA Return: none*/ public void calcGpa(double grade) { credits++; //increase number of credits gpaSum = gpaSum + grade; gpa = gpaSum / credits; } 27

Calling method calcGpa 28 public static void main(String[] args) { Student s1 = new Student(); s1.calcGpa(3.5); //gpa = 3.5 s1.calcGpa(2.4); // gpa = 2.95 }

UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade1, double grade2): void + printData(): void 29

Method printData /*printData Input: noe Description: print instance variables Return: none*/ public void printData() { DecimalFormat df = new DecimalFormat("0.00"); System.out.print("Major: " + major + ”\nClass year: " + classYear + ”\nGPA: " + df.format(gpa)); } 30

Code on Website Open the code Save the code Understand the code Run the code 31

Friday Lab 5 Classes 32