CS141 Programming Assignment #10

Similar documents
System.out.printf("Please, enter the value of the base : \n"); base =input.nextint();

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

CS141 Programming Assignment #6

1 Short Answer (10 Points Each)

CS141 Programming Assignment #5

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

Midterm Examination (MTA)

1 Short Answer (10 Points Each)

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

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Chapter 8 Multi-Dimensional Arrays

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2002

Lecture 2. Two-Dimensional Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CS 101 Spring 2007 Midterm 2 Name: ID:

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

1 Short Answer (7 Points Each)

ESC101 : Fundamental of Computing

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Tutorial about Arrays

Exponentiation and Java

Chapter 8 Multidimensional Arrays

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

CS141 Programming Assignment #8

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Arrays. Eng. Mohammed Abdualal

CS111: PROGRAMMING LANGUAGE II

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

1 Definitions & Short Answer (5 Points Each)

CS Computers & Programming I Review_01 Dr. H. Assadipour

Object Oriented Programming. Java-Lecture 1

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

Introduction to Algorithms and Data Structures

1 Short Answer (10 Points Each)

Array. Array Declaration:

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Example: Monte Carlo Simulation 1

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Ahmadu Bello University, Zaria Department of Computer Science

University of Palestine. Mid Exam Total Grade: 100

AP COMPUTER SCIENCE A

1 Short Answer (5 Points Each)

Fundamentals of Programming Data Types & Methods

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Instructor: Yu Wang 11/16/2012

Supplement: Case Study: Sudoku. For Introduction to Java Programming By Y. Daniel Liang

Practice Problems: Instance methods

3/18/2015. Chapter 19 Sorting and Searching SELECTION SORT SORTING AN ARRAY OF INTEGERS SORTING AN ARRAY OF INTEGERS FILE SELECTIONSORTER.

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Declaring and ini,alizing 2D arrays

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Programming with Java

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

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

CSEN 202: Introduction to Computer Programming Spring term Final Exam

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

Lab Assignment Three

CSCI 1103: File I/O, Scanner, PrintWriter

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

H212 Introduction to Software Systems Honors

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Defining Classes. The basic syntax for defining a class in Java is: class <class-name> [<data-declarations>] [<class-constructors>] [<methods>]

CS 170 Exam 2. Section 004 Fall Name (print): Instructions:

LAB 13: ARRAYS (ONE DIMINSION)

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

ITERATION WEEK 4: EXMAPLES IN CLASS

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

Question: Total Points: Score:

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

2.8. Decision Making: Equality and Relational Operators

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

CS18000: Programming I

Exercise 4: Loops, Arrays and Files

Ch. 6. User-Defined Methods

Arrays, Vectors, Matrices

Lab #10 Multi-dimensional Arrays

Computer Science & Engineering 150A Problem Solving Using Computers

What are Exceptions?

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Transcription:

CS141 Programming Assignment #10 Due Sunday, May 5th. 1) Write a class with the following methods: a) max( int [][] a) Returns the maximum integer in the array. b) min(int [][] a) Returns the minimum integer in the array. c) avergae(int [][] a) Returns the average of integers in the array. d) copy(int [][] a,int [][]b) Copy array a to array b. e) print(int [][] a) Prints the array. 2) Test your class methods you implemented in part 3 on the following array int [][] a = {{10, 5, -6, 0,{11, 8, -2, 7,{4, 6, 2, 1,{9, -5, -3, 50; Make sure to print your answer after each method call. Sol //Student name //Student ID //Assignment# 10 Question #1-2 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: // 1. Returns the maximum integer in the array // 2. Returns the minimum integer in the array. // 3. Returns the average of integers in the array. // 4. Copy array a to array b. // 5. Prints the array. public class Q1 { /* Method to find the maximum value in an array */ public static int max(int [][] a){ int large = a[0][0]; // stores the initial value to compare with if ( large < a[i][j]) large = a[i][j]; return large; /* Method to find the minimum value in an array */ public static int min(int [][] a){ int small = a[0][0]; // stores the initial value to compare with if ( small > a[i][j]) small = a[i][j]; return small; /* Method to find the average of the values in the array */ public static double average(int [][] a){ int sum = 0; // stores the sum of the values in the array sum += a[i][j]; return sum/(double)(a.length*a.length);

/* Method to copy the first array argument into * * the second array argument */ public static void copy(int [][]a, int [][]b){ for(int i = 0; i < a.length; i++){ b[i][j] = a[i][j]; /* Method to print the 2 dimensional array */ public static void print(int [][]a){ for(int i = 0; i < a.length; i++){ System.out.printf("%3d", a[i][j]); public static void main(string[] args) { int [][] a = {{10, 5, -6, 0, {11, 8, -2, 7, {4, 6, 2, 1, {9, -5, -3, 50; System.out.println("The values in the first array are: "); print(a); System.out.println("The maximum value in the array is " + max(a)); System.out.println("The minimum value in the array is " + min(a)); System.out.printf("The average of the values in the array is %.2f\n", average(a)); int [][]b = new int[a.length][a.length]; copy(a, b); System.out.println("The numbers in the second array are: "); print(b); //end main // end class 3) Write a class that reads values of a two-dimensional integer array A[4][4] from the keyboard, and then computes values of integer array B[4], such that B[i] is the smallest value of i-th row of array A. Sol //Student name //Student ID //Assignment# 10 Question #3 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: import java.util.scanner; public class Q3 {

public static void main(string[] args) { int [][]a = new int [4][4]; int []b = new int[4]; readarray(a); findsmall(a,b); printarrays(a, b); /* Method to read the values and store them into the 4x4 matrix */ public static void readarray(int[][]a){ Scanner input = new Scanner(System.in); System.out.println("Enter the values in the 4x4 matrix:"); for(int i = 0; i < a.length; i++) for(int j = 0; j < a[i].length; j++) a[i][j] = input.nextint(); /* Method to find and return the smallest number in an array */ public static int min(int [] a){ int small = a[0]; // Stores an initial value to compare with for (int i = 1; i < a.length; i++) if ( small > a[i]) small = a[i]; return small; /* Method calls method min()to find the smallest value in each * * row and stores it in a second one dimensional array */ public static void findsmall(int [][]a, int []b){ for(int i = 0; i < a.length; i++) b[i] = min(a[i]); /* Method to print the values in the two arrays */ public static void printarrays(int [][]a, int []b){ for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++) System.out.printf("%5d",a[i][j]); System.out.printf(" Smallest is %5d\n", b[i]); 4) Write a class Matrix with the following methods: -Matrix( int row, int col) -Matrix(int [][] m) -print() -fill () -add(matrix a) -sub(matrix a) -mul(matrix a) Sets the matrix array size to row and col Sets the matrix array to the values given by m Prints the matrix array Sets the matrix array values by the user Add the 2 matrices Subtract the two matrices Multiply the two matrices

5) Test your class methods you implemented in part 6 on the following Matrices [ ] [ ] Make sure to print your answer after each method call. Sol //Student name //Student ID //Assignment# 10 Question #4-5 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: //1. Prints the matrix array //2. Sets the matrix array values by the user //3. Add two matrices //4. Subtract two matrices //5. Multiply two matrices import java.util.scanner; class Matrix { private int a[][]; // instance variable to store the matrix /* constructor to set matrix to specific size */ public Matrix(int row, int col) { a = new int[row][col]; /* constructor to set matrix to given values */ public Matrix(int[][] m) { a = new int[m.length][m[0].length]; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) a[i][j] = m[i][j]; /* method to print the matrix */ public void print() { { System.out.printf("%5d", a[i][j]); /* method to set matrix values by the user */ public void fill() { Scanner input = new Scanner(System.in); System.out.println("Enter the values in your matrix: "); a[i][j] = input.nextint();

/* method to add two matrices and return a third matrix containing the result */ public Matrix add(matrix a) { Matrix C = new Matrix(this.a.length, a.a.length); // stores the result if (this.a.length == a.a.length && this.a[0].length == a.a[0].length) { // check if arrays are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) C.a[i][j] = this.a[i][j] + a.a[i][j]; else System.out.println("Array rows and columns are incompatible!!" + " Addition could not be performed"); return C; /* method to subtract two matrices and return a third matrix containing the result */ public Matrix sub(matrix a) { Matrix C = new Matrix(this.a.length, a.a.length); // stores the result if (this.a.length == a.a.length && this.a[0].length == a.a[0].length) { // check if array are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) C.a[i][j] = this.a[i][j] - a.a[i][j]; else System.out.println("Array rows and columns are incompatible!!" + " Subtraction could not be performed"); return C; /* method to multiply two matrices and return a third matrix containing the result */ public Matrix mul(matrix a) { Matrix C = new Matrix(this.a.length, a.a[0].length); // stores the result if (this.a.length == a.a[0].length) { // check if arrays are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) { int sum = 0; for (int k = 0; k < a.a.length; k++) sum += this.a[i][k] * a.a[k][j]; C.a[i][j] = sum; else System.out.println("Array rows and columns are incompatible!!" + " Multiplication could not be performed"); return C; public static void main(string[] args) { int[][] a = { { 3, 4, -4, { 9, 11, 0, { -2, 5, 5 ; Matrix A = new Matrix(a); // first matrix created thru given matrix Matrix B = new Matrix(3, 3);// second matrix created thru specific size B.fill(); System.out.println("The elements in matrix A: "); A.print(); System.out.println("The elements in matrix B:"); B.print();

Matrix C = A.add(B); // stores the result of addition System.out.println("A+B= "); C.print(); Matrix D = A.sub(B); // stores the result of subtraction System.out.println("A-B= "); D.print(); Matrix E = A.mul(B); // stores the result of multiplication System.out.println("A*B= "); E.print(); // end main // end class