Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

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.
Chapter 4 - Control Statements
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
General Computer Science for Engineers CISC 106 Lecture 20 Dr. John Cavazos Computer and Information Sciences 04/08/2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Multiple Choice Solutions True/False a c b e d   T F.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Fundamental Programming Fundamental Programming More on Repetition.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Repetition Statements
Identify the Appropriate Method for Handling Repetition
Lecture 4b Repeating With Loops
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Agenda Warmup Finish 2.4 Assignments
Loop Structures.
LOOPS.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
הרצאה 7: מחרוזות וחתימה של פונקציה
Arrays Declare the Array of 100 elements Notes
Maximization and Minimization Problems
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Eric Roberts and Chris Piech
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Unit 3 - The while Loop - Extending the Vic class - Examples
Patterns to KNOW.
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Sridhar Narayan Java Basics Sridhar Narayan
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Loop Strategies Repetition Playbook.
Fundamental Programming
Arrays Introduction to Arrays Reading for this Lecture:
Michele Weigle - COMP 14 - Spr 04
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

Comparing code JAVABAT WRAPUP

 Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality GENERAL NOTE

 a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string XYBALANCE – NO LOOP public boolean xyBalance(String str) { int tempX = -1; int tempY = -1; tempX = str.lastIndexOf('x'); tempY = str.lastIndexOf('y'); if (tempX > tempY) { return false; } else { return true; }

 Many similar to the following XYBALANCE – LOOP PLUS STUFF public boolean xyBalance(String str) { int lastY = -1; int lastX = -1; //find the last x; make sure there is at least one y after it for (int i = 0; i < str.length(); i++){ if (str.charAt(i) == 'x') lastX = i; else if (str.charAt(i) == 'y') lastY = i; else continue; } if (lastY > lastX) return true; else if (lastY == -1 && lastX == -1) return true; else if ((lastY == -1 && lastX != -1) || (lastX == -1 && lastY != -1)) return false; else return false; }

XYBALANCE – SINGLE LOOP public boolean xyBalance(String str) { boolean isClear = true; for(int i = 0; i < str.length(); i++){ if(str.charAt(i) == 'x'){ isClear = false; } else if(str.charAt(i) == 'y'){ isClear = true; } return isClear; }

XYBALANCE – NESTED LOOP public boolean xyBalance(String str) { int xpos=0; boolean check=true; for( int i=0; i<str.length(); i++) { if (str.charAt(i)=='x') { xpos=i; check=false; for(int j=xpos+1; j<str.length();j++) { if(str.charAt(j)=='y') check=true; } return check; }

 A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread. GET_SANDWICH

 Clean syntax… probably reasonably efficient USING JAVA FUNCTIONALITY public String getSandwich(String str) { final String BREAD = "bread"; int start = str.indexOf(BREAD) + BREAD.length(); int end = str.lastIndexOf(BREAD); if (end <= start) return ""; return str.substring(start, end); }

 Some students wrote their own loops instead of indexOf/ lastIndexOf. Generally harder to read. MAGIC NUMBER public String getSandwich(String str) { int first = str.indexOf("bread"); if(first == -1){ return ""; } String second = str.substring(first+5); int third = second.lastIndexOf("bread"); if(third == -1){ return ""; } String fourth = second.substring(0,third); return fourth; }

 Return true if the array contains, somewhere, three increasing adjacent numbers like.... 4, 5, 6,... or 23, 24, 25. TRIPLE_UP

public boolean tripleUp(int[] nums) { for(int i = 0; i < nums.length - 2; i++){ if(nums[i+1] == nums[i] + 1 && nums[i+2] == nums[i+1] + 1){ return true; } return false; } public boolean tripleUp(int[] nums) { int counter = 0; for(int i=1; i < nums.length; i++){ if (nums[i] == nums[i-1] + 1) { counter++; if (counter == 2) return true; } else { counter = 0; } return false; } TRIPLE UP

 Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}. SCORES CLUMP

public boolean scoresClump(int[] scores) { for(int i = 0; i < scores.length-2; i++){ if(scores[i+2]-scores[i]<=2) return true; } return false; } MAKE USE OF WHAT YOU KNOW ABOUT THE PROBLEM

public boolean scoresClump(int[] scores) { if(scores.length < 3){return false;} int x = scores[0]; int y = scores[1]; int z = scores[2]; for(int i = 0; i < scores.length ; i++){ if(i+2 < scores.length){ x = scores[i]; y = scores[i +1]; z = scores[i +2]; } else {return false;} if((Math.abs(x - y) <= 2) && (Math.abs(x - z) <=2) ){ return true; } return false; } EXTRA VARIABLES (NOT ALWAYS BAD)

public boolean scoresClump(int[] scores) { for(int i = 0; i < scores.length - 2; i++) { if(Math.abs(scores[i] - scores[i+1]) <= 2 && Math.abs(scores[i] - scores[i+2]) <= 2 && Math.abs(scores[i+1] - scores[i+2]) <= 2) return true; } return false; } OK IF DON’T TRUST INPUT DATA