College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Written by: Dr. JJ Shepherd
{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.
Chapter 4: Writing Classes
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
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!
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
© A+ Computer Science - Chicken yeller = new Chicken();
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Applications Development
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Objects as a programming concept
Building Java Programs
Chapter 4: Writing Classes
Methods.
Something about Java Introduction to Problem Solving and Programming 1.
Writing Methods.
Multiple Choice -answer the easiest question 1st
Lesson A4 – Object Behavior
Classes & Objects: Examples
Defining methods and more arrays
© A+ Computer Science - OOP © A+ Computer Science -
CS 200 Primitives and Expressions
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Week 4 Lecture-2 Chapter 6 (Methods).
Introduction to Java Brief history of Java Sample Java Program
Lecture 5- Classes, Objects and Methods
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Methods/Functions.
A+ Computer Science AP Review 2019 AP CS A EXAM
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction and encapsulation. Class Design - Design and implement a class; Design an interface. Programming Constructs - Class declarations; Parameter declarations. Program Analysis - Understand and modify existing code. Standard Data Structures - Simple data types (int, boolean, double) ; Classes.

String x = new String(“hello”); Monster m = new Monster();

{ int fun = 99; } Any variable defined inside of braces, only exists within those braces. That variable has a scope limited to those braces.

When you need many methods to have access to the same variable, you make that variable an instance variable. The scope of an instance variable is the entire class where that variable is defined.

public class InstanceVars { private int one = 9, two = 7; public int add() { int sum = one + two; return sum; } public int subtract() { int difference = one – two; return difference; } public class InstanceVarsRunner { public static void main(String args[]) { InstanceVars test = new InstanceVars(); int total = test.add(); int diff = test.subtract(); System.out.println (total); System.out.println (diff); } OUTPUT 16 2 one and two are instance variables. We need to use them in both the add and the subtract methods. sum and difference are local variables. They only exist inside the methods that declare them. Once the method is complete these variables are de-allocated from memory.

When you need only one method to have access to a variable, you should make that variable a local variable. The scope of a local variable is limited to the method where it is defined.

Refer to the InstanceVars class on the previous slide. Because the scope of sum and difference is their methods, we could reuse these names in our main. In change 1, we try to print sum and difference. You will get an error, because these variables do not exist in the main. In change 2, notice that we have to declare them as int in again in the main. Try this change in Dr. Java. Change 1: public static void main(String args[]) { InstanceVars test = new InstanceVars(); int total = test.add(); int diff = test.subtract(); System.out.println (sum); System.out.println (difference); } Change 2: public static void main(String args[]) { InstanceVars test = new InstanceVars(); int sum = test.add(); int difference = test.subtract(); System.out.println (sum); System.out.println (difference); }

class Triangle { private int sidea, sideb, sidec; public Triangle() { sidea=0; sideb=0; sidec=0; } Triangle A = new Triangle(); A constructor that does not have parameters, is called a default constructor. Java provides a class with a default constructor automatically if one is not written by the programmer.

//constructor method example class Triangle { private int sidea, sideb, sidec; public Triangle() { sidea=0; sideb=0; sidec=0; } public String toString() { return (sidea + " " + sideb + " " + sidec); } public class ConstructorOne { public static void main ( String[] args ) { Triangle test = new Triangle(); System.out.println (test); } A default constructor takes no arguments (parameters)!

A parameter is a channel used to send information to a method. setColor is a method of the Graphics class. void setColor( Color theColor) window.setColor( Color.red );

class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } Triangle A = new Triangle(3,4,5);

have same name as class have no return type initialize instance variables

//constructor method example class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public String toString() { return(sidea + " " + sideb + " " + sidec); } public class ConstructorTwo { public static void main ( String[] args ) { Triangle test = new Triangle(5,6,7); System.out.println (test); } This constructor takes in 3 parameters. The numbers represent the 3 side lengths of the triangle. Run it!

Scanner keyboard = new Scanner(System.in); System.out.print("Enter a integer :: "); int num = keyboard.nextInt(); return method

Return methods perform some action and return a result to the calling location. double root = Math.sqrt(144); root would be assigned the value double roundup = Math.ceil(3.121); roundup would be assigned the value 4.0.

public class Return1 { public int twice( int x ) //this is a return method { return 2*x; } public class Return1Runner { public static void main (String [] args) { Return1 demo = new Return1(); System.out.println("First value returned :: " + demo.twice(25) ); }

//return method example import static java.lang.System.*; public class Return2 { public int willReturnHere( int x ) { return x*x*x; } public class Return2Runner { public static void main ( String[] args ) { Return2 demo = new Return2(); demo.willReturnHere(3); //this is how you use a return method correctly //int answer = demo.willReturnHere(3); //System.out.println(answer); }

public Triangle() { sidea=0; sideb=0; sidec=0; } Constructor methods are methods that set the properties of the an object to an initial state.

public void setNums( int n1, int n2 ) { one=n1; two=n2; } Modifier methods are methods that change the properties of the an object.

public void getNumOne() { return one; } Accessor methods are methods that retrieve the properties of the an object.

public class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public void setSides(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public String toString() { return "sides " + sidea + " " + sideb + " " + sidec; } Create 2 classes, Triangle and OOP. Run this and make sure you understand it. public class OOP { public static void main ( String[] args ) { Triangle test = new Triangle(5,6,7); System.out.println(test); test.setSides(4,4,4); System.out.println(test); test.setSides(1,56,22); System.out.println(test); }