Lab Exercise 1. Objectives: Part 1. Introduction

Similar documents
Instructions. Objectives:

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

CAT.woa/wa/assignments/eclipse

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Programming with Java

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Instructions. Objectives:

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

b. Suppose you enter input from the console, when you run the program. What is the output?

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

Midterm Examination (MTA)

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

CSIS 10A Assignment 9 Solutions

H212 Introduction to Software Systems Honors

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Chapter 3: Classes and Objects. CSC2010 Lab 9. Java Programming. Copyright 2000 W. W. Norton & Company. All rights reserved. FROM THE BEGINNING

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

CS111: PROGRAMMING LANGUAGE II

Fundamentals of Programming Data Types & Methods

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Beginning Programming (Java) Test 2/Version 1 CMSC 1513 Spring 2013

Oct Decision Structures cont d

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Object Oriented Programming. Java-Lecture 6 - Arrays

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

Loops. CSE 114, Computer Science 1 Stony Brook University

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

Java Coding 3. Over & over again!

Welcome to the Primitives and Expressions Lab!

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.

Arrays. Eng. Mohammed Abdualal

Introduction to Computer Science Unit 2. Notes

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

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Question 1 [20 points]

CSC 1051 Data Structures and Algorithms I

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Object Oriented Programming. Java-Lecture 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Exam 2 Spring Id Name

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

King Saud University College of Computer and Information Sciences Computer Science Department

Lecture 8 " INPUT " Instructor: Craig Duckett

Exercise 0.1 (Review from lectures no submission required)

CSC 1051 Data Structures and Algorithms I

CS 101 Fall 2006 Midterm 3 Name: ID:

MIDTERM REVIEW. midterminformation.htm

Topic 11 Scanner object, conditional execution

Chapter 2. Elementary Programming

Controls Structure for Repetition

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Control Structures: if and while A C S L E C T U R E 4

Software and Programming 1

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Date: Dr. Essam Halim

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

2.2 - Making Decisions

Programming Problems 22nd Annual Computer Science Programming Contest

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Example Program. public class ComputeArea {

Building Java Programs

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CPSC 219 Extra review and solutions

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

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

CSC 1051 Data Structures and Algorithms I

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CEN 414 Java Programming

Java Identifiers, Data Types & Variables

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Claremont McKenna College Computer Science

Lecture Set 4: More About Methods and More About Operators

Repetition, Looping. While Loop

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

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.

Introduction to Computer Science Unit 2. Notes

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment

Objects and Classes User-defined classes, creating objects, accessing object s members

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Exceptions Handeling

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

CSCI 1103: File I/O, Scanner, PrintWriter

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

A token is a sequence of characters not including any whitespace.

Transcription:

Objectives: king Saud University College of Computer &Information Science CSC111 Lab Object II All Sections - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To describe objects and classes, and use classes to model objects. To use UML graphical notation to describe classes and objects. To demonstrate how to define classes and create objects. To create objects using default constructors. To access objects via object reference variables. To define a reference variable using a reference type. To access an object s data and methods using the object member access operator (.) To understand how to create and use methods, parameters and return values. Lab Exercise 1 Introduction In this lab we will define a new class and call it TV and we will initialize three properties for this class. As we go, we will keep adding new methods to the TV class such as turnon() and turnoff()methods for tuning the TV off and on. Part 1 Write a class and call it TV with two integer variables channel and volumelevel,and one boolean variable on. Write the variables by completing the following pseudo code: public class TV {

Part 2 In the previous part you have defined a new class with instance variables to store the channel, the volume level and the status of the TV. In this part you will use methods to modify the status of the TV (i.e., instance variable on) rather than modifying the instance variable directly. Add to the TV class you defined in part 1 two methods for modifying the instance variable on which are turnon() and turnoff(). When you want to turn the TV on you call turnon() (which will set on to true) and turnoff() for turning the TV off (which will set on to false). (Note that both methods do NOT return any value and do NOT have any parameter) public class TV { //turn on the tv public void turnon() { on = true; //turn off the tv public /* returntype */ /* method name */() { //set on to false

Part 3 Now write a method ison()to check weather the TV is on or off (i.e. return the value of the variable on). See the example below: public class TV { //turn on the tv public void turnon() { on = true; //turn off the tv public /* returntype */ /* method name */() { //set on to false // returns true of tv is turned on /*modifier */ /* returntype */ ison(){ //return value of on Part 4 Now add two more methods to modify volume level. Each of the two methods takes an integer as a parameter. volumelevelup(int vol) and volumeleveldown(int vol) should raise or lower the volume level by vol number of levels. For example if volumelevel is 5 and you called volumelevelup(3) the new volumelevel should be equal to 5 + 3 = 8. The volume level must be between 0 and 8.

public class TV { //turn on the tv public void turnon() { on = true; //turn off the tv public /* returntype */ /* method name */() { //set on to false // returns true of tv is turned on /*modifier */ /* returntype */ ison(){ //return value of on // raises volume up such that it does not exceed 8 public void volumelevelup(int vol) { /* calculate new volume level */ if (/* value of new volume level less than 8 */){ /* set volumelevel value to new volume level */ // lowers volume down such that it does not go below 0 /*modifier */ /* returntype */ /* method name */( /*parameters*/) { //method body similar to volumelevelup Part 5 Now add two more methods that take an integer as a parameter and modify channel using the value of that integer. channelup(int ch) and channeldown(int ch) should increment or decrement the channel by ch number of channels. For example if channel is 5 and you called

channelup(10) the new channel value should be equal to 5 + 10 = 15. The channel must be between 0 and 100. public class TV { //turn on the tv public void turnon() { on = true; //turn off the tv public /* returntype */ /* method name */() { //set on to false // returns true of tv is turned on /*modifier */ /* returntype */ ison(){ //return value of on // raises volume up such that it does not exceed 8 public void volumelevelup(int vol) { /* calculate new volume level */ if (/* value of new volume level less than 8 */){ /* set volumelevel value to new volume level */ // lowers volume down such that it does not go below 0 /*modifier */ /* returntype */ /* method name */( /*parameters*/) { //method body similar to volumelevelup // channelup method goes here // channeldown method goes here

Part 6 Add a new method tostring() that returns all current information of the TV itself. It should return the TV info in a format similar to the following example: TV is on and current channel is 5 and current volume level is 8. Part 7 Test your program by creating a new class TestTV with a main method. In this class you do the following: 1- Create a TV object. 2- Turn the tv on. 3- Go to channel 20 (java always initializes your int data members to 0, so the value of the channel when you create the TV object is 0). 4- Raise the volume by 4. 5- Check if the TV is on and if so print its information using method tostring(). 6- Turn the TV off. Sample Run TV is on and current channel is 20 and current volume level is 4. public class TestTV { public static void main(string[] args) { /* create the object tv1 */ tv1.turnon();//turn on the tv // go to channel 20 // raise volume by 4 if (/* check if tv is on */) { System.out.println(/*get tv info using tostring() */ Part 8

Draw the UML for class TV (Hint: see UML for class LinearEquation in next exercise). TV Lab Exercise 2 (Optional) Design a class named LinearEquation for a 2 x 2 system of linear equations: ax + by = e cx + dy = f A system of linear equations can be solved using Cramer s rule as following: x =./012 3/014 y = 320.4 3/014 The class contains: Data fields a, b, c, d, e, and f. A method named issolvable() that returns true if ad - bc is not 0. Methods solvex() and solvey() that return the solution for the equation. Draw the UML diagram for the class and then implement the class. Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the solution. If ad - bc is 0, report that The system has no solution. Sample Run 1

Enter a, b, c, d, e, f: 9 4 3-5 -6-21 x is -2.0 and y is 3.0 Sample Run 2 Enter a, b, c, d, e, f: 1 2 2 4 5 5 The system has no solution UML Unlike previous program, in this program we are going to solve everything at once, i.e., write the whole class at once. First phase is to design your program as an OOP program. Draw UML diagrams for the two classes, LinearEquation and TestLinearEquation. LinearEquation a: double b: double c: double d: double e: double f: double issolvable(): boolean solvex(): double solvey(): double TestLinearEquation main(): void

Partial Solution Now write your program. Construct two classes LinearEquation and TestLinearEquation in the same file. import java.util.scanner; public class TestLinearEquation { public static void main(string[] args) { Scanner input = new Scanner(System.in); /* create an object of type equation */ System.out.print("Enter a, b, c, d, e, f: "); equation.a = input.nextdouble(); //read remaining fields b, c, d, e, f if (/*check if equation is solvable */) { System.out.println("x is " + /* call method solvex */ + " and y is " + /*call method solvey */ ); else { System.out.println("The system has no solution"); System.exit(0); class LinearEquation { //data members public /* return type */ issolvable() { return /* boolean expression to check if solvable */; /*modifier*/ /*return type*/ solvex() { double x = /* calculate the solution */ /* return the solution */ //write method solvey()