Chapter 6 Lab Classes and Objects

Similar documents
Chapter 6 Lab Classes and Objects

Anatomy of a Java Program: Comments

Task #1 The if Statement, Comparing Strings, and Flags

Chapter 3 Lab Decision Structures

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

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.

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Tips from the experts: How to waste a lot of time on this assignment

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

Chapter 5 Lab Methods

Software and Programming 1

CS 101 Fall 2006 Midterm 3 Name: ID:

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.

Chapter 5 Lab Methods

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

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

System.out.print(); Scanner.nextLine(); String.compareTo();

Lecture 8 " INPUT " Instructor: Craig Duckett

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Objects as a programming concept

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 5 Lab Methods

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

APCS Semester #1 Final Exam Practice Problems

Course Outline. Introduction to java

4. Java Project Design, Input Methods

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Chapter 4 Defining Classes I

Implementing an ADT with a Class

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Software and Programming 1

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Chapter 9 Lab Text Processing and Wrapper Classes

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Using Classes. GEEN163 Introduction to Computer Programming

A A B U n i v e r s i t y

Recommended Group Brainstorm (NO computers during this time)

Advanced Programming - CS239

Software and Programming 1

Supplementary Test 1

Chapter 11: Create Your Own Objects

EECS168 Exam 3 Review

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

Arrays & Classes. Problem Statement and Specifications

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Defining Classes and Methods

Unit 4: Classes and Objects Notes

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

public static boolean isoutside(int min, int max, int value)

Guessing Game with Objects

Lecture 10. Instructor: Craig Duckett

Java and OOP. Part 2 Classes and objects

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Exam 2 Spring Id Name

CS 302 Week 9. Jim Williams

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

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

Final Exam Practice. Partial credit will be awarded.

Assignment 1 due Monday at 11:59pm

Text User Interfaces. Keyboard IO plus

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Express Yourself. Writing Your Own Classes

Chapter 1 Lab Algorithms, Errors, and Testing

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

COMP 250 Winter 2011 Reading: Java background January 5, 2011

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

Control Statements: Part 1

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Lesson 12: OOP #2, Accessor Methods (W03D4)

Lec 3. Compilers, Debugging, Hello World, and Variables

CSCI 355 Lab #2 Spring 2007

Programming II (CS300)

Object Communication

CS 211: Existing Classes in the Java Library

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

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Lesson 10: Quiz #1 and Getting User Input (W03D2)

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Fundamentals of Programming Data Types & Methods

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Software Practice 1 Basic Grammar

Program 12 - Spring 2018

CSIS 10B Lab 2 Bags and Stacks

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

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Object Oriented Programming. Java-Lecture 1

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Tips from the experts: How to waste a lot of time on this assignment

AP CS Unit 4: Classes and Objects Programs

Instance Method Development Demo

Transcription:

Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that take arguments Be able to instantiate an object Be able to use calls to instance methods to access and change the state of an object Introduction Everyone is familiar with a television. It is the object we are going to create in this lab. First we need a blueprint. All manufacturers have the same basic elements in the televisions they produce as well as many options. We are going to work with a few basic elements that are common to all televisions. Think about a television in general. It has a brand name (i.e. it is made by a specific manufacturer). The television screen has a specific size. It has some basic controls. There is a control to turn the power on and off. There is a control to change the channel. There is also a control for the volume. At any point in time, the television s state can be described by how these controls are set. We will write the television class. Each object that is created from the television class must be able to hold information about that instance of a television in fields. So a television object will have the following attributes: manufacturer. The manufacturer attribute will hold the brand name. This cannot change once the television is created, so will be a named constant. screensize. The screensize attribute will hold the size of the television screen. This cannot change once the television has been created so will be a named constant. poweron. The poweron attribute will hold the value true if the power is on, and false if the power is off. channel. The channel attribute will hold the value of the station that the television is showing. volume. The volume attribute will hold a number value representing the loudness (0 being no sound). These attributes become fields in our class. The television object will also be able to control the state of its attributes. These controls become methods in our class. setchannel. The setchannel method will store the desired station in the channel 29

power. The power method will toggle the power between on and off, changing the value stored in the poweron field from true to false or from false to true. increasevolume. The increasevolume method will increase the value stored in the volume field by 1. decreasevolume. The decreasevolume method will decrease the value stored in the volume field by 1. getchannel. The getchannel method will return the value stored in the channel getvolume. The getvolume method will return the value stored in the volume getmanufacturer. The getmanufacturer method will return the constant value stored in the MANUFACTURER getscreensize. The getscreensize method will return the constant value stored in the SCREEN_SIZE We will also need a constructor method that will be used to create an instance of a Television. These ideas can be brought together to form a UML (Unified Modeling Language) diagram for this class as shown below. Television -MANUFACTURER: String -SCREEN_SIZE: int -poweron: boolean -channel: int -volume: int +Television(brand: String, size: int): +setchannel (station: int): void +power( ): void +increasevolume( ): void +decreasevolume( ): void +getchannel( ): int +getvolume( ): int +getmanufacturer( ): String +getscreensize( ): int Class Name Attributes or fields Methods + public - private Data type returned 30

Task #1 Creating a New Class 1. In a new file, create a class definition called Television. 2. Put a program header (comments/documentation) at the top of the file // The purpose of this class is to model a television // Your name and today s date 3. Declare the 2 constant fields listed in the UML diagram. 4. Declare the 3 remaining fields listed in the UML diagram. 5. Write a comment for each field indicating what it represents. 6. Save this file as Television.java. 7. Compile and debug. Do not run. Task #2 Writing a Constructor 1. Create a constructor definition that has two parameters, a manufacturer s brand and a screen size. These parameters will bring in information 2. Inside the constructor, assign the values taken in from the parameters to the corresponding fields. 3. Initialize the poweron field to false (power is off), the volume to 20, and the channel to 2. 4. Write comments describing the purpose of the constructor above the method header. 5. Compile and debug. Do not run. Task #3 Methods 1. Define accessor methods called getvolume, getchannel, getmanufacturer, and getscreensize that return the value of the corresponding 2. Define a mutator method called setchannel accepts a value to be stored in the channel 3. Define a mutator method called power that changes the state from true to false or from false to true. This can be accomplished by using the NOT operator (!). If the boolean variable poweron is true, then!poweron is false and vice versa. Use the assignment statement poweron =!poweron; to change the state of poweron and then store it back into poweron (remember assignment statements evaluate the right hand side first, then assign the result to the left hand side variable. 4. Define two mutator methods to change the volume. One method should be called increasevolume and will increase the volume by 1. The other method should be called decreasevolume and will decrease the volume by 1. 5. Write javadoc comments above each method header. 6. Compile and debug. Do not run. 31

Task #4 Running the application 1. You can only execute (run) a program that has a main method, so there is a driver program that is already written to test out your Television class. Copy the file TelevisionDemo.java (see code listing 3.1) from the Student CD or as directed by your instructor. Make sure it is in the same directory as Television.java. 2. Compile and run TelevisionDemo and follow the prompts. 3. If your output matches the output below, Television.java is complete and correct. You will not need to modify it further for this lab. OUTPUT (boldface is user input) A 55 inch Toshiba has been turned on. What channel do you want? 56 Channel: 56 Volume: 21 Too loud!! I am lowering the volume. Channel: 56 Volume: 15 Task #5 Creating another instance of a Television 1. Edit the TelevisionDemo.java file. 2. Declare another Television object called portable. 3. Instantiate portable to be a Sharp 19 inch television. 4. Use a call to the power method to turn the power on. 5. Use calls to the accessor methods to print what television was turned on. 6. Use calls to the mutator methods to change the channel to the user s preference and decrease the volume by two. 7. Use calls to the accessor methods to print the changed state of the portable. 8. Compile and debug this class. 9. Run TelevisionDemo again. 10. The output for task #5 will appear after the output from above, since we added onto the bottom of the program. The output for task #5 is shown below. OUTPUT (boldface is user input) A 19 inch Sharp has been turned on. What channel do you want? 7 Channel: 7 Volume: 18 Code Listing 3.1 (TelevisionDemo.java) /** This class demonstrates the Television class*/ import java.util.scanner; public class TelevisionDemo { public static void main(string[] args) 32

{ //create a Scanner object to read from the keyboard Scanner keyboard = new Scanner (System.in); } } //declare variables int station; //the user s channel choice //declare and instantiate a television object Television bigscreen = new Television("Toshiba", 55); //turn the power on bigscreen.power(); //display the state of the television System.out.println("A " + bigscreen.getscreensize() + bigscreen.getmanufacturer() + " has been turned on."); //prompt the user for input and store into station System.out.print("What channel do you want? "); station = keyboard.nextint(); //change the channel on the television bigscreen.setchannel(station); //increase the volume of the television bigscreen.increasevolume(); //display the the current channel and volume of the television System.out.println("Channel: " + bigscreen.getchannel() + " Volume: " + bigscreen.getvolume()); System.out.println("Too loud!! I am lowering the volume."); //decrease the volume of the television //display the current channel and volume of the television System.out.println("Channel: " + bigscreen.getchannel() + " Volume: " + bigscreen.getvolume()); System.out.println(); //for a blank line //HERE IS WHERE YOU DO TASK #5 33