CMP 167 Programming Methods One

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
CSCI 160 Midterm Review Rasanjalee DM.
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
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.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Topics Instance variables, set and get methods Encapsulation
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter VII: Arrays.
Looping Examples I.
The need for Programming Languages
Section 2.6 Linked List StringLog ADT Implementation
Passing Objects to Methods
Arrays 2/4 By Pius Nyaanga.
Intro To Classes Review
Advanced Programming in Java
CS Week 14 Jim Williams, PhD.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
CS Week 13 Jim Williams, PhD.
Recursion 12-Nov-18.
Arrays Declare the Array of 100 elements Notes
המשך תכנות מונחה עצמים תרגול מס' 9.
Building Java Programs Chapter 7
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Review of Classes and Arrays
Recursion 2-Dec-18.
Recursion 2-Dec-18.
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Building Java Programs
CS150 Introduction to Computer Science 1
Recursion 29-Dec-18.
Generic Classes and Methods
Lecture 13: Two-Dimensional Arrays
Basics of OOP A class is the blueprint of an object.
Code Refresher Test #1 Topics:
2/24/2019.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Review of Classes and Arrays
CIS 199 Final Review.
Fundamental Programming
Recursion 23-Apr-19.
Objects with ArrayLists as Attributes
Review of Classes and Arrays
Review for Midterm 3.
First Semester Review.
Presentation transcript:

CMP 167 Programming Methods One Final Review

Question One quadratic_function(1,5,6); Solution: Roots -2.0 and -3.0 Use the code to write the results. If there are errors or infinite loops please indicate. Solution: Roots -2.0 and -3.0 public static void quadratic_function(double a, double b, double c){ double secondRoot = 0, firstRoot = 0; double determinant = (b*b)-(4*a*c); double sqrt = Math.sqrt(determinant); if(determinant>0){ firstRoot = (-b + sqrt)/(2*a); secondRoot = (-b - sqrt)/(2*a); System.out.println("Roots are "+ firstRoot +" and "+secondRoot); }else if(determinant == 0){ System.out.println("Root is "+(-b + sqrt)/(2*a)); } (b) quadratic_function(1,6,5); Solution: Roots -1.0 and -5.0 (c) quadratic_function(1,2,3);

Question Two conditionPrint(10); (b) conditionPrint(5); a=10 i=0 i=3 i=6 i=9 Use the code to write to write the results. If there are errors or infinite loops please indicate. public static void conditionPrint(int a){ if(a % 5 == 0){ System.out.println("a="+a); for(int i = 0; i < a; i=i+3){ System.out.println("i="+i); } else{ for(int i = a; i > 0; i--){ System.out.println("zip" + i); (b) conditionPrint(5); a=5 i=0 i=3 (c) conditionPrint(6); zip6 zip5 zip4 zip3 zip2 zip1 a=6

Question Three sum28([2, 3, 2, 2, 4, 2]); true Use the code to write to write the results. If there are errors or infinite loops please indicate. true public boolean sum28(int[] nums) { int count = 0; Arrays.sort(nums); for(int i = 0; i < nums.length; i++) { if(nums[i] == 2) count++; } return (count == 4)?true:false; (b) sum28([2, 3, 2, 2, 4, 2, 2]); false (c) sum28([]); false

Question Four public static int findMax(int a, int b) { return (a > b)?a:b; } Write a static method named findMax, that will take in 2 int values and return the max of the two values.

Question Five The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in. sleepIn(false, false) → true sleepIn(true, false) → false sleepIn(false, true) → true public boolean sleepIn(boolean weekday, boolean vacation) { boolean sleep = false; if (weekday == false && vacation == true) sleep = true; if (weekday == false && vacation == false) if (weekday == true && vacation == true) return sleep; }

Question Six Complete a public class to represent a Garden as described below Create the following private member variables int numFlowers String ownerName boolean hasTrees Create a default constructor and another which accepts all variables as input parameters. Create getter and setter methods for the variables Create the equals (Object o) method for the Garden object, such that it returns true if the values of all the members of both the calling object and the passed in object match. Return false if any values do not match. Create a toString() methods which returns the current values for numFlowers, and ownerName()

Question Six Private member variables private int numFlowers; private String ownerName; private boolean hasTrees;

Question Six Constructors public Garden() { this.numFlowers = 0; this.ownerName = ""; this.hasTrees = false; } public Garden(int numFlowers, String ownerName, boolean hasTrees) { setNumFlowers(numFlowers); setOwnerName(ownerName); setHasTrees(hasTrees);

Another Way for the parameterized Constructor public Garden(int numFlowers, String ownerName, boolean hasTrees) { this.numFlowers = numFlowers; this.ownerName = ownerName; this.hasTrees = hasTrees; }

Question Six Getters and Setters public int getNumFlowers() { return numFlowers; } public void setNumFlowers(int numFlowers) { this.numFlowers = numFlowers; public String getOwnerName() { return ownerName; public void setOwnerName(String ownerName) { this.ownerName = ownerName; public boolean isHasTrees() { return hasTrees; public void setHasTrees(boolean hasTrees) { this.hasTrees = hasTrees;

Question Six Equals @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; if (getClass() != obj.getClass()) { Garden other = (Garden) obj; if (hasTrees != other.hasTrees) { if (numFlowers != other.numFlowers) { if (ownerName == null) { if (other.ownerName != null) { } else if (!ownerName.equals(other.ownerName)) {

Question Six toString() public String toString(){ return “Number of flowers” + getNumberFlowers() + “and store owner name is “ + getOwnerName(); }

Question Seven Given an array of ints length 3, return the sum of all the elements. public int sum3(int[] nums) { return nums[0] + nums[1] + nums[2]; }

Question Eight Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. middleWay([1, 2, 3], [4, 5, 6]) → [2, 5] middleWay([7, 7, 7], [3, 8, 0]) → [7, 8] middleWay([5, 2, 9], [1, 4, 5]) → [2, 4] public int[] middleWay(int[] a, int[] b) { return new int[]{a[1],b[1]}; }