CSC1590 Java Assignment 2-0. User Defined Classes

Similar documents
Classes. Classes (continued) Syntax The general syntax for defining a class:

1. An operation in which an overall value is computed incrementally, often using a loop.

CS111: PROGRAMMING LANGUAGE II

CS122/Sec. 2 Week No. 3 Dilce Gozuyasli Sevi Durdu Due date: March 7, 08. Notes of February 26, 08. Review of class Circle UML diagram

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Chapter 4: Control Structures I

H212 Introduction to Software Systems Honors

5) (4 points) What is the value of the boolean variable equals after the following statement?

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS111: PROGRAMMING LANGUAGE II

CS 1337 Computer Science II Page 1

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Recap: Assignment as an Operator CS 112 Introduction to Programming

Classes and Objects: A Deeper Look

Manipulating One-dimensional Arrays

Lab Exercise 1. Objectives: Part 1. Introduction

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Building Java Programs

CS 112 Introduction to Programming

Chapter 2. Elementary Programming

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 2: Basic Elements of Java

Object Oriented Programming. Java-Lecture 1

Practice Problems: Instance methods

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11


CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

JAVA OPERATORS GENERAL

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CS 1083 Introduction to Programming I for Computer Scientists Final Solutions

CSC 1051 Data Structures and Algorithms I

Spring 2010 Java Programming

Java Coding 3. Over & over again!

Building Java Programs

Building Java Programs

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

COMP 110 Programming Exercise: Simulation of the Game of Craps

Give one example where you might wish to use a three dimensional array

CSC 1214: Object-Oriented Programming

Building Java Programs

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

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Programming with Java

Chapter 1b Classes and Objects

Classes and Objects: A Deeper Look

CS1083 Week 2: Arrays, ArrayList

Building Java Programs

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

AP COMPUTER SCIENCE A

CS 101 Spring 2007 Midterm 2 Name: ID:

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

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

Controls Structure for Repetition

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CS 101 Fall 2006 Midterm 3 Name: ID:

CompSci 125 Lecture 11

Data Structure and Programming Languages

Building Java Programs

Data dependent execution order data dependent control flow

Building Java Programs

Intro to Programming in Java Practice Midterm

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

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

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

The Java language has a wide variety of modifiers, including the following:

Arrays. Eng. Mohammed Abdualal

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Some Sample AP Computer Science A Questions - Solutions

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

Container Vs. Definition Classes. Container Class

Recitation: Loop Jul 7, 2008

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Full file at

Lecture Set 4: More About Methods and More About Operators

AP CS Unit 3: Control Structures Notes

1 Short Answer (10 Points Each)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Software Development With Java CSCI

Building Java Programs

Objects and Classes: Working with the State and Behavior of Objects

Loops and Expression Types

Lab Activity Plan. John Dalbey CPE /30/2013

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Chapter 13: Arrays of Objects

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Selenium Class 9 - Java Operators

University of Palestine. Mid Exam Total Grade: 100

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Example: Monte Carlo Simulation 1

ITI Introduction to Computing II

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Transcription:

CSC1590 Java Assignment 2-0 User Defined Classes This is Chapter 8 in your textbooks. We are now starting to create more complex programs and classes. In this example today we will have a class that consists of a series of methods and no main. We then will write a test program that uses all of the methods we declare in the first class. The example we re using today is about setting and maintaining a clock. This is all in your textbook and you can get the code out of the textbook folder on your shared drive. You need to get the clock class and the TestProgClock class out of that folder and create a separate class for each where you copy in the info. I also copied and pasted them below if you prefer to copy them this way. This is the first class called Clock no main program exists so you cannot just run this program public class Clock private int hr; //store hours private int min; //store minutes private int sec; //store seconds //Default constructor //Postcondition: hr = 0; min = 0; sec = 0 public Clock() settime(0, 0, 0); //Constructor with parameters, to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds public Clock(int hours, int minutes, int seconds) settime(hours, minutes, seconds);

//Method to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds public void settime(int hours, int minutes, int seconds) if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; //Method to return the hours //Postcondition: The value of hr is returned. public int gethours() return hr; //Method to return the minutes //Postcondition: The value of min is returned. public int getminutes() return min; //Method to return the seconds //Postcondition: The value of sec is returned. public int getseconds() return sec; //Method to print the time //Postcondition: Time is printed in the form hh:mm:ss public void printtime() if (hr < 10) System.out.print("0"); System.out.print(hr + ":"); if (min < 10)

System.out.print("0"); System.out.print(min + ":"); if (sec < 10) System.out.print("0"); System.out.print(sec); //Method to increment the time by one second //Postcondition: The time is incremented by one second //If the before-increment time is 23:59:59, the time //is reset to 00:00:00. public void incrementseconds() sec++; if (sec > 59) sec = 0; incrementminutes(); //increment minutes //Method to increment the time by one minute //Postcondition: The time is incremented by one minute. //If the before-increment time is 23:59:53, the time //is reset to 00:00:53. public void incrementminutes() min++; if (min > 59) min = 0; incrementhours(); //increment hours //Method to increment the time by one hour //Postcondition: The time is incremented by one hour. //If the before-increment time is 23:45:53, the time //is reset to 00:45:53. public void incrementhours() hr++; if (hr > 23) hr = 0; //Method to compare the two times //Postcondition: Returns true if this time is equal

to // otherclock; otherwise returns false. public boolean equals(clock otherclock) return (hr == otherclock.hr && min == otherclock.min && sec == otherclock.sec); //Method to copy the time //Postcondition: The instance variables of otherclock are // copied into the corresponding data members // of this time. // hr = otherclock.hr; min = otherclock.min; // sec = otherclock.sec. public void makecopy(clock otherclock) hr = otherclock.hr; min = otherclock.min; sec = otherclock.sec; //Method to return a copy of the time //Postcondition: A copy of the object is created // and a reference of the copy is returned. public Clock getcopy() Clock temp = new Clock(); temp.hr = hr; temp.min = min; temp.sec = sec; return temp; This is the second program called TestProgClock it does include a main so you can run this. import java.util.*; public class TestProgClock static Scanner console = new Scanner(System.in); public static void main(string[] args)

Clock myclock = new Clock(5, 4, 30); //Line 1 Clock yourclock = new Clock(); //Line 2 int hours; //Line 3 int minutes; //Line 4 int seconds; //Line 5 System.out.print("Line 6: myclock: "); //Line 6 myclock.printtime(); //Line 7 System.out.println(); //Line 8 System.out.print("Line 9: yourclock: "); //Line 9 yourclock.printtime(); //Line 10 System.out.println(); //Line 11 yourclock.settime(5, 45, 16); //Line 12 System.out.print("Line 13: After setting " + "the time, yourclock: "); //Line 13 yourclock.printtime(); //Line 14 System.out.println(); //Line 15 if (myclock.equals(yourclock)) //Line 16 System.out.println("Line 17: Both the " + "times are equal."); //Line 17 else //Line 18 System.out.println("Line 19: The two " + "times are not " + "equal."); //Line 19 20 System.out.print("Line 20: Enter the hours, " + "minutes, and seconds: "); //Line hours = console.nextint(); //Line 21 minutes = console.nextint(); //Line 22 seconds = console.nextint(); //Line 23 System.out.println(); //Line 24 myclock.settime(hours, minutes, seconds); //Line 25 System.out.print("Line 26: New time of " + "myclock: "); //Line 26 myclock.printtime(); //Line 27 System.out.println(); //Line 28 myclock.incrementseconds(); //Line 29 System.out.print("Line 30: After " + "incrementing the time by " + "one second, myclock: "); //Line 30

myclock.printtime(); //Line 31 System.out.println(); //Line 32 yourclock.makecopy(myclock); //Line 33 System.out.print("Line 34: After copying " + "myclock into yourclock, " + "yourclock: "); //Line 34 yourclock.printtime(); //Line 35 System.out.println(); //Line 36 //end main Assignment 1. Get the example program TestProgClock to work correctly on your computer. 2. Add 3 methods to the Clock class. One will subtract a second. One will subtract a minute. The third will subtract an hour. 3. Change the TestProgClock class so that it will call each of the three new methods and print out the clock decremented by one second, one minute, and one hour.