Advanced Java Concepts Unit 3: Stacks and Queues

Size: px
Start display at page:

Download "Advanced Java Concepts Unit 3: Stacks and Queues"

Transcription

1 Advanced Java Concepts Unit 3: Stacks and Queues Stacks are linear collections in which access is completely restricted to just one end, called the top. Stacks adhere to a last-in, first-out protocol (LIFO). Think of a stack of plates; the last plate placed on the stack is the first one you take off. Java does have a Stack Class; here are some of its methods. Method Boolean empty() E peek() E pop() void push( E element) int size() Description Precondition: the stack is not empty Returns the object at the top of the stack (but does not remove it) Precondition: the stack is not empty Removes and returns the object at the top of the stack Adds an element to the top of the stack Note. Java implements the Stack class as a subclass of the Vector class which implements the List interface. You will need an import statement whenever you need to use this class. 1. Why do people criticize Java s stack class? What alternative could have been used? 2. What does this code snippet display? 3. This code compiles. Will there be a run-time error? If yes, explain. If no error, what is displayed? Stack<Integer> st = new Stack<Integer>(); st.push( 1 ); st.push( 2 ); st.push( 3 ); System.out.println( st.pop() ); System.out.println( st.pop() ); System.out.println( st.pop() ); import java.util.*; public class TryStacks { public static void main(string[] args) { Stack<String> st = new Stack<String>(); st.push( "A" ); st.push( "B" ); System.out.println( st.pop() ); System.out.println( st.peek() ); System.out.println( st.pop() ); Page 1

2 One area where stacks are used is in memory management. The exact details of how computer memory is managed depend on the computer s operating system and the programming language. However, the following simplified description can be useful. Whenever a method is called, an activation record is created. This activation record includes any parameters, local variables, and probably other things I m not aware of. This activation record is placed on the stack an area of program memory reserved for this purpose. When the method is complete, the activation record is popped off the stack. The top most activation record on the stack is the method currently being executed. 4. A program has a main method and two other methods: method A and method B. The figure to the right shows the current state of the program s stack. Which statement is TRUE? a) methoda is currently running. b) methodb is currently running. c) The main method is currently running. d) There is not enough information to answer this. 5. Given the current state of the program s stack, what do you know about methoda? Note. While nothing may be technically correct for you, it is not the correct answer for this question. methodb methoda main methoda methoda main top of stack top of stack 6. Program. Here is an algorithm for converting a non-negative base 10 number to a base 2 number. 1. Find the remainder when the number is divided by two. Push the remainder onto a stack. 2. Number = number / 2 3. Go back to step 1. Keep repeating as long as the number is greater than zero. 4. Print out the contents of the stack (i.e. print the topmost first, then the second and so on). Write a program that implements the above algorithm. After you get the basic version working, clean it up as follows: Instruct the user to enter an integer between 0 and Convert the number into base 2. Add leading zeroes (if needed) so that there is a total of 16 zeroes and ones. When you print the result, add a blank space between every four bits. For example, If the user enters 513, your program should display If the user enters 60013, your program should display If the user enters 0, your program should display Your prompt to the user must be: System.out.println( "Integer?" ); Page 2

3 7. Program. Complete the reverse method. It takes a stack and returns a copy that is in reverse order. Postcondition: the state of the parameter matches its state before the method was called. Your solution can only use stacks, no array lists, no linked lists, etc. public static void main( String [] args ){ Stack<Integer> stack = new Stack<Integer>(); stack.push( 4 ); stack.push( 5 ); stack.push( 6 ); stack.push( 7 ); Stack<Integer> new_stack = reverse( stack ); System.out.println( stack ); // [4, 5, 6, 7] System.out.println( new_stack ); // [7, 6, 5, 4] public static Stack<Integer> reverse( Stack<Integer> st ){... Evaluating Arithmetic Expressions. Before we discuss how stacks are used in evaluating arithmetic expressions, we need to discuss the three ways an expression can be written. Form Description Example infix Each operator is located between its operands This is the form you know and love. prefix The operator is placed to the left of the operands. This is also known as Polish notation. postfix The operator is placed to the right of the operands. This is also known as Reverse Polish notation Infix notation sometimes requires the use of parentheses. Prefix and postfix notation do not require (and therefore do not use) parentheses. In fact, in prefix and postfix notation the order of operations is completely determined by the order of the operators. Some early (1970 s and 80 s) handheld calculators required that users enter the expressions in reverse Polish notation. Please read the Wikipedia articles, (prefix) and (postfix) on these topics. To evaluate an expression in prefix notation, you scan the statement from left to right. As soon as you see two adjacent operands, you perform the operation specified by the operator immediately preceding it. Then you replace the operator and two operands with the result. Keep repeating until you have a result. IMPORTANT. If the result of a particular operation is a negative number, then the resulting operand is negative (you are not adding a subtraction operator to the expression). Here are a couple of examples. + * scanning left to right we find the operands 3 and 4 and the operator right before them is * so we multiply them together and replace them with so we add 12 and 5 together 17 is the result Page 3

4 Another example / scanning left to right the first pair of operands we find is 8 and 2 so we divide them and replace / 8 2 with replace with replace with is an operand, add -6 and 3 together -3 is the result Problems 8 to 12. Evaluate the following prefix expressions. 8) / ) / + * ) * ) + 10 / * ) * To evaluate an expression in postfix notation, you scan the statement from left to right. As soon as you see two adjacent operands followed by an operator, you perform the operation specified and replace the operator and two operands with the result. Keep repeating until you have a result For example: * scanning left to right we find the operands 6 and 7 and the operator right after them is + so we add them together and replace them with * multiple 13 by 2 26 is the result Another example * - 2 / scanning left to right the first pair of operands followed by an operator we find so we replace that with * - 2 / replace 6 9 * with / replace with / -51 is an operand, divide -51 by is the result Problems 13 to 17. Evaluate the following postfix expressions. 13) * 14) * 15) / + * 16) 9 2 * 3 5 * + 17) / * * Page 4

5 I ll leave it up to you to find out how to convert prefix and postfix expression into the corresponding infix expressions and vice versa. Problems 18 to 22. Rewrite each statement from infix form to prefix and postfix forms. YOU MUST KEEP THE OPERANDS IN THE SAME ORDER (sorry for shouting). Infix Prefix Postfix * * * (2 + 3)(4 5) (5 2) 23. Program. Complete the Eval class. Given a valid postfix expression, the evalpostfix method will return the value of that expression. The method should be able to handle: *, /, +, -. and ^ (exponent). You may assume that there is exactly one space between each operator/operand. However, there may be any number of spaces at the start and end. public class Eval{ public static int evalpostfix( String s ){ Call the trim method to remove any leading or trailing spaces Call the split method to create a String array that consists of the numbers and operands in the string. All spaces should be removed. Create a string stack. Iterate through the array. If the element is a number, push it onto a stack. If the element is an operator, pop the last two elements off the stack, evaluate the expression and push the result onto the stack. When you are finished iterating through the array, the size of the stack should be one. Pop that value off the stack, convert it to an int and return it. Some sample test data: String s1 = " * + "; System.out.println( Eval.evalPostfix( s1 ) ); // 22 String s2 = " * "; System.out.println( Eval.evalPostfix( s2 ) ); // 77 String s3 = " / "; System.out.println( Eval.evalPostfix( s3 ) ); // 9 String s4 = " * / "; System.out.println( Eval.evalPostfix( s4 ) ); // -12 Some hints and suggestions are on the next page. Page 5

6 Suggestions for program described on the previous page: trim and split are methods of the String class. Write a helper method that determines if a string is a number. This can be done by using the Character.isDigit method though remember that the number could be positive or negative. Or you could write a method that checks by using a try-catch thingee. Does this sound familiar? Maybe? (Voice getting higher each time.) Write other helper methods as appropriate. Queues are linear collections in which elements can only be added to one end and you can only remove elements from the other end. Think of a line to buy tickets. When people arrive they go to the end of the line; the person at the head of the line is the next person to be served. This is called a first-in, first-out protocol (FIFO). Java has a Queue interface to support this abstract data type. Important Methods of the Queue Interface Method Description boolean add( E element ) Adds element to the rear of the queue and returns true if room is available or returns false otherwise. boolean isempty() Returns true if the queue is empty. E peek() Returns the object at the front of the queue or null if the queue is empty. E remove() Precondition: the queue is not empty. Removes and returns the object at the front of the queue. int size() Returns the number of objects in the queue. The LinkedList class implements the Queue interface. For example: import java.util.*; public class Example { public static void main(string[] args) { Queue<String> q = new LinkedList<String>(); q.add( "B" ); // prints A // prints B Page 6

7 Priority Queues are similar to queues in that items are added to the end of the queue and removed from the head of the queue. They are different in that more important elements go to the head of the line. For example, imagine an emergency room at a hospital. As people arrive they get in line and wait their turn. However, if someone comes in with a life-threatening problem, they go to the head of the line. Java has a PriorityQueue class that implements the Queue interface. The default constructor places objects in the queue according to their natural order which is defined by how they implement the Comparable interface (i.e. the compareto method). The item with the smallest value is removed first. For example, in the String class A is less than B so it will go to the head of the queue. import java.util.*; public class Example { public static void main(string[] args) { PriorityQueue<String> q = new PriorityQueue<String>(); q.add( "B" ); q.add( "H" ); q.add( "C" ); // prints A // prints B // prints C // prints H Note. Suppose you have a class named Dog that does not implement Comparable. The following code will compile: PriorityQueue<Dog> q = new PriorityQueue<Dog>(); Dog d1 = new Dog( "hank" ); Dog d2 = new Dog( "bob" ); q.add( d1 ); q.add( d2 ); However, when you try to run it you will get the following runtime error: java.lang.classcastexception: Dog cannot be cast to java.lang.comparable Page 7

8 Queues and Priority Queues 24. What is displayed? Queue<Integer> q = new LinkedList<Integer>(); q.add( 14 ); q.add( 5 ); q.add( 12 ); 25. What is displayed? PriorityQueue<Integer> q = new PriorityQueue<Integer>(); q.add( 14 ); q.add( 5 ); q.add( 12 ); 26. What is displayed? Queue<String> q = new LinkedList<String>(); q.add( "M" ); q.add( "G" ); q.add( "K" ); while (! q.isempty() ) 27. What is displayed? PriorityQueue<String> q = new PriorityQueue<String>(); q.add( "M" ); q.add( "G" ); q.add( "K" ); while (! q.isempty() ) 28. What is displayed? PriorityQueue<String> q = new PriorityQueue<String>(); q.add( "apple" ); q.add( "a" ); q.add( "xray" ); The ASCII code for A is 65. q.add( "Adam" ); The ASCII code for a is 97. q.add( "ZOO!" ); while (! q.isempty() ) Page 8

9 29. Program. Complete the removethis method. It should remove every occurrence of s from the queue but leave all the other elements in their original locations. In the method make one pass through the queue, removing elements as you go. If the element does not match s, then add it back to the queue. import java.util.*; public class RemoveQueue { public static void main(string[] args) { Queue<String> q = new LinkedList<String>(); q.add( "C" ); q.add( "B" ); q.add( "B" ); System.out.println( q ); // [A, A, C, A, B, B, A] removethis( q, "A" ); // [C, B, B] System.out.println( q ); s){ public static void removethis( Queue<String> qe, String Program. This program models an emergency room and it has three classes. The Patient class obviously represents a patient at a hospital. public class Patient implements Comparable<Patient>{ private String name; private int condition; // 1 = worst, 10 = not too bad public Patient( String n, int c ){ name = n; condition = c; public String tostring(){ return name; public int compareto( Patient other ){ /* patients are compared based on their condition. Return a negative number if this patient s condition is more severe than the other patient. Return a zero if the conditions are the same. Return a positive number if the other guy is hurting more. */ Page 9

10 The second class models the line at the emergency room. Patients with more life-threatening ailments go to the head of the line. Please complete the class. import java.util.*; public class ERqueue { private PriorityQueue<Patient> q; public ERqueue(){ q = new PriorityQueue<Patient>(); public void schedule( Patient p ){ // add the patient to the priority queue public String treatnext(){ // remove the next patient from the priority queue // return a string like has been healed and replace with the patient s name public String getplaceinline( Patient pat ){ // returns a string like "(name) is #1 out of 3 patients" // assume that each patient has a different name // the state of the queue is unchanged Here s some sample test code. ERqueue er = new ERqueue(); Patient p1 = new Patient( "Sarah", 5 ); Patient p2 = new Patient( "Johnny", 10 ); Patient p3 = new Patient( "Mike", 3 ); Patient p4 = new Patient( "Alex", 8 ); Patient p5 = new Patient( "Shelley", 1 ); Patient p6 = new Patient( "Pat", 2 ); er.schedule( p1 ); er.schedule( p2 ); er.schedule( p3 ); er.schedule( p4 ); er.schedule( p5 ); er.schedule( p6 ); System.out.println( er.getplaceinline( p1 ) ); System.out.println( er.getplaceinline( p3 ) ); System.out.println( " " ); System.out.println( er.treatnext() ); System.out.println( er.treatnext() ); System.out.println( " " ); System.out.println( er.getplaceinline( p3 ) ); // Sarah is #4 out of 6 patients. // Mike is #3 out of 6 patients. // Shelley has been healed // Pat has been healed // Mike is #1 out of 4 patients. Page 10

Chapter 3: Restricted Structures Page 1

Chapter 3: Restricted Structures Page 1 Chapter 3: Restricted Structures Page 1 1 2 3 4 5 6 7 8 9 10 Restricted Structures Chapter 3 Overview Of Restricted Structures The two most commonly used restricted structures are Stack and Queue Both

More information

Module 2 Stacks and Queues: Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which

More information

7.1 Our Current Model

7.1 Our Current Model Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.

More information

Data Structures and Algorithms V22.0102. Otávio Braga

Data Structures and Algorithms V22.0102. Otávio Braga Data Structures and Algorithms V22.0102 Otávio Braga We use a stack When an operand is read, output it When an operator is read Pop until the top of the stack has an element of lower precedence Then push

More information

Java Interview Questions and Answers

Java Interview Questions and Answers 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

More information

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1 Stacks (and Queues) 1 Stacks Stack: what is it? ADT Applications Implementation(s) 2 CSCU9A3 1 Stacks and ueues A stack is a very important data structure in computing science. A stack is a seuence of

More information

Stacks. Linear data structures

Stacks. Linear data structures Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

More information

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

More information

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson Outline Computer Science 1 Stacks Mike Jacobson Department of Computer Science University of Calgary Lecture #12 1 2 Applications Array-Based Linked List-Based 4 Additional Information Mike Jacobson (University

More information

Stacks. Data Structures and Data Types. Collections

Stacks. Data Structures and Data Types. Collections Data Structures and Data Types Data types Set values. Set operations on those values. Some are built in to Java: int, double, char,... Most are not: Complex, Picture, Charge, Stack, Queue, Graph,... Data

More information

Introduction to Data Structures

Introduction to Data Structures Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate

More information

Software Engineering Techniques

Software Engineering Techniques Software Engineering Techniques Low level design issues for programming-in-the-large. Software Quality Design by contract Pre- and post conditions Class invariants Ten do Ten do nots Another type of summary

More information

10CS35: Data Structures Using C

10CS35: Data Structures Using C CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

More information

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

More information

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever! ECE 551 C++ Programming, Data structures, and Algorithms Abstract Data Type: Stack Last In First Out (LIFO) 1 2 2 1 4 3 1 3 4 Stacks in Programming Worst line ever! 5 3 1 5 Stacks are not useful for waiting

More information

14 Stacks, Queues, And Linked Lists

14 Stacks, Queues, And Linked Lists 14-1 Java Au Naturel by William C. Jones 14-1 14 Stacks, Queues, And Linked Lists Overview This chapter requires that you have a solid understanding of arrays (Chapter Seven) and have studied Exceptions

More information

D06 PROGRAMMING with JAVA

D06 PROGRAMMING with JAVA Cicles Formatius de Grau Superior Desenvolupament d Aplicacions Informàtiques D06 PROGRAMMING with JAVA Ch20 Data Structures I PowerPoint presentation, created by Angel A. Juan - ajuanp(@)gmail.com, for

More information

Collaboration policy. Where to get help Email (but no code in email) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Collaboration policy. Where to get help Email (but no code in email) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates Collaboration policy Programs: Do not use someone else s code unless specifically authorized Exceptions Code from course materials OK [cite source] Coding with partner OK after first assignment [stay tuned]

More information

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1 Definition Course: Programming II - Abstract Data Types The ADT Stack The ADT Stack is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit insertions

More information

Introduction to Stacks

Introduction to Stacks Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack. What is a Stack? Stack is a data structure in which data is added

More information

Common Data Structures

Common Data Structures Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees

More information

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues 1. 2004 Goodrich, Tamassia

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues 1. 2004 Goodrich, Tamassia This lecture Abstract data types Stacks Queues ADTs, Stacks, Queues 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations

More information

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/ 22c:31 Algorithms Ch3: Data Structures Hantao Zhang Computer Science Department http://www.cs.uiowa.edu/~hzhang/c31/ Linear Data Structures Now we can now explore some convenient techniques for organizing

More information

26 Integers: Multiplication, Division, and Order

26 Integers: Multiplication, Division, and Order 26 Integers: Multiplication, Division, and Order Integer multiplication and division are extensions of whole number multiplication and division. In multiplying and dividing integers, the one new issue

More information

Chapter 5 Instructor's Manual

Chapter 5 Instructor's Manual The Essentials of Computer Organization and Architecture Linda Null and Julia Lobur Jones and Bartlett Publishers, 2003 Chapter 5 Instructor's Manual Chapter Objectives Chapter 5, A Closer Look at Instruction

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

More information

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything

More information

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions COMP209 Object Oriented Programming Designing Classes 2 Mark Hall Programming by Contract (adapted from slides by Mark Utting) Preconditions Postconditions Class invariants Programming by Contract An agreement

More information

MEP Y9 Practice Book A

MEP Y9 Practice Book A 1 Base Arithmetic 1.1 Binary Numbers We normally work with numbers in base 10. In this section we consider numbers in base 2, often called binary numbers. In base 10 we use the digits 0, 1, 2, 3, 4, 5,

More information

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

More information

Lecture J - Exceptions

Lecture J - Exceptions Lecture J - Exceptions Slide 1 of 107. Exceptions in Java Java uses the notion of exception for 3 related (but different) purposes: Errors: an internal Java implementation error was discovered E.g: out

More information

DATA STRUCTURE - STACK

DATA STRUCTURE - STACK DATA STRUCTURE - STACK http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Copyright tutorialspoint.com A stack is an abstract data type ADT, commonly used in most programming

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Inner classes, RTTI, Tree implementation Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 29, 2010 G. Lipari (Scuola Superiore Sant

More information

Chapter 2: Elements of Java

Chapter 2: Elements of Java Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction

More information

Java CPD (I) Frans Coenen Department of Computer Science

Java CPD (I) Frans Coenen Department of Computer Science Java CPD (I) Frans Coenen Department of Computer Science Content Session 1, 12:45-14:30 (First Java Programme, Inheritance, Arithmetic) Session 2, 14:45-16:45 (Input and Programme Constructs) Materials

More information

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have 8.6 Rational Exponents 8.6 OBJECTIVES 1. Define rational exponents 2. Simplify expressions containing rational exponents 3. Use a calculator to estimate the value of an expression containing rational exponents

More information

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016 CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Linda Shapiro Registration We have 180 students registered and others who want to get in. If you re thinking of dropping

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2015 Week 2b: Review of Week 1, Variables 16 January 2015 Birkbeck

More information

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system. http://www.tutorialspoint.com/java/java_quick_guide.htm JAVA - QUICK GUIDE Copyright tutorialspoint.com What is Java? Java is: Object Oriented Platform independent: Simple Secure Architectural- neutral

More information

Sequential Data Structures

Sequential Data Structures Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year

More information

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: 15.1-15.3. Data Structures

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: 15.1-15.3. Data Structures 1.00 Lecture 35 Data Structures: Introduction Stacks, Queues Reading for next time: Big Java: 15.1-15.3 Data Structures Set of reusable classes used in algorithms, simulations, operating systems, applications

More information

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

More information

Introduction to Programming System Design. CSCI 455x (4 Units)

Introduction to Programming System Design. CSCI 455x (4 Units) Introduction to Programming System Design CSCI 455x (4 Units) Description This course covers programming in Java and C++. Topics include review of basic programming concepts such as control structures,

More information

PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC QUESTION BANK PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

More information

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

CHAPTER 4 ESSENTIAL DATA STRUCTRURES CHAPTER 4 ESSENTIAL DATA STRUCTURES 72 CHAPTER 4 ESSENTIAL DATA STRUCTRURES In every algorithm, there is a need to store data. Ranging from storing a single value in a single variable, to more complex

More information

Universidad Carlos III de Madrid

Universidad Carlos III de Madrid Universidad Carlos III de Madrid Algorithms and Data Structures (ADS) Bachelor in Informatics Engineering Computer Science Department Lists, Stacks and Queues. Authors: Isabel Segura Bedmar April 2011

More information

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language Translation Translating to Java Introduction to Computer Programming The job of a programmer is to translate a problem description into a computer language. You need to be able to convert a problem description

More information

Building a Multi-Threaded Web Server

Building a Multi-Threaded Web Server Building a Multi-Threaded Web Server In this lab we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous

More information

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution 16. Recursion COMP 110 Prasun Dewan 1 Loops are one mechanism for making a program execute a statement a variable number of times. Recursion offers an alternative mechanism, considered by many to be more

More information

CompSci-61B, Data Structures Final Exam

CompSci-61B, Data Structures Final Exam Your Name: CompSci-61B, Data Structures Final Exam Your 8-digit Student ID: Your CS61B Class Account Login: This is a final test for mastery of the material covered in our labs, lectures, and readings.

More information

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

More information

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1 Definition Course: Programming II - Abstract Data Types The ADT Queue The ADT Queue is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit addition

More information

Inside the Java Virtual Machine

Inside the Java Virtual Machine CS1Bh Practical 2 Inside the Java Virtual Machine This is an individual practical exercise which requires you to submit some files electronically. A system which measures software similarity will be used

More information

AP Computer Science Java Mr. Clausen Program 9A, 9B

AP Computer Science Java Mr. Clausen Program 9A, 9B AP Computer Science Java Mr. Clausen Program 9A, 9B PROGRAM 9A I m_sort_of_searching (20 points now, 60 points when all parts are finished) The purpose of this project is to set up a program that will

More information

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction bstract ata Types Previous lectures: algorithms and their efficiency analysis. oming lectures: data structures In this lecture: bstract data types Ts as a design tool Examples: integer T, List T ata Types

More information

Fractions. If the top and bottom numbers of a fraction are the same then you have a whole one.

Fractions. If the top and bottom numbers of a fraction are the same then you have a whole one. What do fractions mean? Fractions Academic Skills Advice Look at the bottom of the fraction first this tells you how many pieces the shape (or number) has been cut into. Then look at the top of the fraction

More information

J a v a Quiz (Unit 3, Test 0 Practice)

J a v a Quiz (Unit 3, Test 0 Practice) Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points

More information

Java Basics: Data Types, Variables, and Loops

Java Basics: Data Types, Variables, and Loops Java Basics: Data Types, Variables, and Loops If debugging is the process of removing software bugs, then programming must be the process of putting them in. - Edsger Dijkstra Plan for the Day Variables

More information

Introduction to Java

Introduction to Java Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high

More information

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving Section 7 Algebraic Manipulations and Solving Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving Before launching into the mathematics, let s take a moment to talk about the words

More information

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority) Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

More information

HW3: Programming with stacks

HW3: Programming with stacks HW3: Programming with stacks Due: 12PM, Noon Thursday, September 18 Total: 20pts You may do this assignment with one other student. A team of two members must practice pair programming. Pair programming

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

2.6 Exponents and Order of Operations

2.6 Exponents and Order of Operations 2.6 Exponents and Order of Operations We begin this section with exponents applied to negative numbers. The idea of applying an exponent to a negative number is identical to that of a positive number (repeated

More information

No Solution Equations Let s look at the following equation: 2 +3=2 +7

No Solution Equations Let s look at the following equation: 2 +3=2 +7 5.4 Solving Equations with Infinite or No Solutions So far we have looked at equations where there is exactly one solution. It is possible to have more than solution in other types of equations that are

More information

COMPUTER SCIENCE. Paper 1 (THEORY)

COMPUTER SCIENCE. Paper 1 (THEORY) COMPUTER SCIENCE Paper 1 (THEORY) (Three hours) Maximum Marks: 70 (Candidates are allowed additional 15 minutes for only reading the paper. They must NOT start writing during this time) -----------------------------------------------------------------------------------------------------------------------

More information

Part 3: GridWorld Classes and Interfaces

Part 3: GridWorld Classes and Interfaces GridWorld Case Study Part 3: GridWorld Classes and Interfaces In our example programs, a grid contains actors that are instances of classes that extend the Actor class. There are two classes that implement

More information

Data Structures in the Java API

Data Structures in the Java API Data Structures in the Java API Vector From the java.util package. Vectors can resize themselves dynamically. Inserting elements into a Vector whose current size is less than its capacity is a relatively

More information

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit. Scanner The Scanner class is intended to be used for input. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit. For example, suppose the input

More information

Computer Programming I

Computer Programming I Computer Programming I COP 2210 Syllabus Spring Semester 2012 Instructor: Greg Shaw Office: ECS 313 (Engineering and Computer Science Bldg) Office Hours: Tuesday: 2:50 4:50, 7:45 8:30 Thursday: 2:50 4:50,

More information

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius Programming Concepts Practice Test 1 1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius 2) Consider the following statement: System.out.println("1

More information

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers.

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers. 1.4 Multiplication and (1-25) 25 In this section Multiplication of Real Numbers Division by Zero helpful hint The product of two numbers with like signs is positive, but the product of three numbers with

More information

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be: Lecture 7 Picking Balls From an Urn The problem: An urn has n (n = 10) balls numbered from 0 to 9 A ball is selected at random, its' is number noted, it is set aside, and another ball is selected from

More information

arrays C Programming Language - Arrays

arrays C Programming Language - Arrays arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

Semantic Analysis: Types and Type Checking

Semantic Analysis: Types and Type Checking Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

More information

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

More information

CS 2112 Spring 2014. 0 Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

CS 2112 Spring 2014. 0 Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions CS 2112 Spring 2014 Assignment 3 Data Structures and Web Filtering Due: March 4, 2014 11:59 PM Implementing spam blacklists and web filters requires matching candidate domain names and URLs very rapidly

More information

1.4 Compound Inequalities

1.4 Compound Inequalities Section 1.4 Compound Inequalities 53 1.4 Compound Inequalities This section discusses a technique that is used to solve compound inequalities, which is a phrase that usually refers to a pair of inequalities

More information

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists Data Structures Using C++ 2E Chapter 5 Linked Lists Doubly Linked Lists Traversed in either direction Typical operations Initialize the list Destroy the list Determine if list empty Search list for a given

More information

X86-64 Architecture Guide

X86-64 Architecture Guide X86-64 Architecture Guide For the code-generation project, we shall expose you to a simplified version of the x86-64 platform. Example Consider the following Decaf program: class Program { int foo(int

More information

Chapter 8: Bags and Sets

Chapter 8: Bags and Sets Chapter 8: Bags and Sets In the stack and the queue abstractions, the order that elements are placed into the container is important, because the order elements are removed is related to the order in which

More information

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract -- In this paper, a software application that features the visualization of commonly used

More information

Data Structure [Question Bank]

Data Structure [Question Bank] Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

More information

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) ----------------------------------------- =============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com

More information

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc. WA2099 Introduction to Java using RAD 8.0 Student Labs Web Age Solutions Inc. 1 Table of Contents Lab 1 - The HelloWorld Class...3 Lab 2 - Refining The HelloWorld Class...20 Lab 3 - The Arithmetic Class...25

More information

CS 111 Classes I 1. Software Organization View to this point:

CS 111 Classes I 1. Software Organization View to this point: CS 111 Classes I 1 Software Organization View to this point: Data Objects and primitive types Primitive types operators (+, /,,*, %). int, float, double, char, boolean Memory location holds the data Objects

More information

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix: APPENDIX D NUMBER SYSTEMS You will learn about the following in this appendix: The four important number systems in computing binary, octal, decimal, and hexadecimal. A number system converter program

More information

Base Conversion written by Cathy Saxton

Base Conversion written by Cathy Saxton Base Conversion written by Cathy Saxton 1. Base 10 In base 10, the digits, from right to left, specify the 1 s, 10 s, 100 s, 1000 s, etc. These are powers of 10 (10 x ): 10 0 = 1, 10 1 = 10, 10 2 = 100,

More information

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011 CS101 Lecture 11: Number Systems and Binary Numbers Aaron Stevens 14 February 2011 1 2 1 3!!! MATH WARNING!!! TODAY S LECTURE CONTAINS TRACE AMOUNTS OF ARITHMETIC AND ALGEBRA PLEASE BE ADVISED THAT CALCULTORS

More information

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues Chapter 20 Lists, Stacks, Queues, and Priority Queues Objectives q To explore the relationship between interfaces and classes in the Java Collections Framework hierarchy ( 20.2). q To use the common methods

More information

Stack Allocation. Run-Time Data Structures. Static Structures

Stack Allocation. Run-Time Data Structures. Static Structures Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

More information

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. Study Group 1 Variables and Types 1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. 2. What does the byte 00100110 represent? 3. What is the purpose of the declarations

More information

PROBLEMS (Cap. 4 - Istruzioni macchina)

PROBLEMS (Cap. 4 - Istruzioni macchina) 98 CHAPTER 2 MACHINE INSTRUCTIONS AND PROGRAMS PROBLEMS (Cap. 4 - Istruzioni macchina) 2.1 Represent the decimal values 5, 2, 14, 10, 26, 19, 51, and 43, as signed, 7-bit numbers in the following binary

More information

CMSC 202H. ArrayList, Multidimensional Arrays

CMSC 202H. ArrayList, Multidimensional Arrays CMSC 202H ArrayList, Multidimensional Arrays What s an Array List ArrayList is a class in the standard Java libraries that can hold any type of object an object that can grow and shrink while your program

More information

Section 1.5 Exponents, Square Roots, and the Order of Operations

Section 1.5 Exponents, Square Roots, and the Order of Operations Section 1.5 Exponents, Square Roots, and the Order of Operations Objectives In this section, you will learn to: To successfully complete this section, you need to understand: Identify perfect squares.

More information

Analysis of a Search Algorithm

Analysis of a Search Algorithm CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth,

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information