LAB 13: ARRAYS (ONE DIMINSION)

Similar documents
LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

LAB 12: ARRAYS (ONE DIMINSION)

LAB 5: SELECTION STATEMENTS

Object Oriented Programming. Java-Lecture 6 - Arrays

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Methods CSC 121 Fall 2016 Howard Rosenthal

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Java Classes: Math, Integer A C S L E C T U R E 8

Methods CSC 121 Spring 2017 Howard Rosenthal

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Methods CSC 121 Fall 2014 Howard Rosenthal

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Computer programming Code exercises [1D Array]

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

A Foundation for Programming

Array. Array Declaration:

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Fundamentals of Programming Data Types & Methods

Java Classes: Random, Character A C S L E C T U R E 6

H212 Introduction to Software Systems Honors

LAB 1: FAMILIARITY WITH NETBEANS IDE ENVIRONMENT

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

BİLGE KÖROĞLU. Lecture Notes (May 2 4, 2007) METHOD DECOMPOSITION and ARRAYS

2.2 - Making Decisions

Object Oriented Programming with JAVA

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Passing Array to Methods

EECS168 Exam 3 Review

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Introduction to Computer Science Unit 2. Notes

CO Java SE 8: Fundamentals

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Module 7: Arrays (Single Dimensional)

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

CS 101 Fall 2006 Midterm 1 Name: ID:

Ryerson University Vers HAL6891A-09 School of Computer Science CPS109 Midterm Test Fall 09 page 1 of 7

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

Introduction to Software Development (ISD) Week 3

Program 12 - Spring 2018

H212 Introduction to Software Systems Honors

Arrays. Eng. Mohammed Abdualal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

COE 211/COE 212 Computer Programming/Engineering Programming. Welcome to The Comprehensive Final Exam Monday June

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

COURSE: Introduction to JAVA Programming GRADE(S): UNIT 1: Introduction to Applets NATIONAL STANDARDS: ALL STUDENTS

4. Java language basics: Function. Minhaeng Lee

Midterm Examination (MTA)

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Chapter 5 Lab Methods

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

CS 101, Spring 2016 March 22nd Exam 2

COMPUTER APPLICATIONS

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

CSIS 10B Lab 2 Bags and Stacks

Tutorial about Arrays

ARRAYS. Java Programming

Introduction to programming using Python

Programming Exercise 7: Static Methods

St. Edmund Preparatory High School Brooklyn, NY

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

COMPUTER APPLICATIONS

Activity 3: Data Types

Introduction to Computer Science Unit 2. Notes

ICSE 2015 COMPUTER APPLICATIONS. Md. Zeeshan Akhtar

Using Parallel Arrays. Parallel Array Example

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

1 Short Answer (10 Points Each)

Introduction to Programming Using Java (98-388)

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

COMP 110 Programming Exercise: Simulation of the Game of Craps

Conditional Programming

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Introduction to Java

EASY

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Java Coding 3. Over & over again!

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Instructions for CharStringReturnInt Testing Template

EXPERIMENTS IN COMPUTER SCIENCE

CS111: PROGRAMMING LANGUAGE II

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Course Outline. Introduction to java

Programming Project: ArrayFun

CE151 ASSIGNMENT

Transcription:

Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: As a second Lab on Chapter 7, this lab continues practicing the Chapter 7 theory with students, with using ideas of arrays. The students will learn how to pass an array to method and how return an array from the methods. There are some exercises, through which they will understand the concept learn related to methods. Theory Review (5 minutes) Passing arrays to method Java uses pass by value to pass arguments to a method. There are important differences between passing a value of variables of primitive data types and passing arrays. For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method. For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument. Example 1 Method that accepts an array and prints its values public static void printarraay(int [] a) Invoke the method by passing an array { for(int i=0;i<a.length;i++) System.out.print(a[i]+ " "); int []arr= {10,20,15,30,50}; printarraay(arr); } CPCS-202 - The Lab Note Lab 13 Page 1

Example 2 Method that enters values into array and returns it. public static int []readarray() { Scanner sc=new Scanner(System.in); System.out.print("Enter number of elements: "); int numvalues=sc.nextint(); int []arr=new int[numvalues]; System.out.println("Enter "+ numvalues+ " elements "); for(int i=0;i<numvalues;i++) } arr[i]=sc.nextint(); return arr; Invoke the method and receive the returned array int []list=readarraay(); Practice Activity with Lab Instructor (10 minutes) (Find the smallest element) Write a method that finds the smallest element in an array of double values using the following header: public static double min(double[] array) Write a test program that prompts the user to enter ten numbers, invokes this method to return the minimum value, and displays the minimum value. Here is a sample run of the program: CPCS-202 - The Lab Note Lab 13 Page 2

Solution: 1. Open NetBeans and create a new project 2. Create a new java main class and write its name as ReturnMinValue 3. Write the following code inside the class. Short Exercise (5 minutes) (Find the largest index of the small element) Rewrite the above method min to return the largest index of the smallest element. CPCS-202 - The Lab Note Lab 13 Page 3

Individual Activities: (60 minutes) 1. (Average an array) Write two overloaded methods that return the average of an array with the following headers: public static int average(int[] array) public static double average(double[] array) Write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value. 2. (Sorted?) Write the following method that returns true if the list is already sorted in increasing order. public static boolean issorted(int[] list) Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. 3. (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateduplicates(int[] list) Write a test program that reads in ten integers, invokes the method, and displays the result. Here is the sample run of the program: CPCS-202 - The Lab Note Lab 13 Page 4

4. (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to store words, as follows: // Add any words you wish in this array String[] words = {"write", "that",...}; 5. (Pattern recognition: consecutive four equal numbers) Write the following method that tests whether the array has four consecutive numbers with the same value. public static boolean isconsecutivefour(int[] values) Write a test program that prompts the user to enter a series of integers and displays if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size i.e., the number of values in the series. Here are sample runs: CPCS-202 - The Lab Note Lab 13 Page 5