coe318 Lab 2 ComplexNumber objects

Similar documents
coe318 Lab 1 Introduction to Netbeans and Java

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.

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

JAVA OPERATORS GENERAL

CIT 590 Homework 6 Fractions

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CS 201, Fall 2016 Sep 28th Exam 1

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Selenium Class 9 - Java Operators

Place your name tag here

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Methods. Eng. Mohammed Abdualal

Java Simple Data Types

Java Simple Data Types

H212 Introduction to Software Systems Honors

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CMPS 11 Intermediate Programming Midterm 2 Review Problems

Spring, 2014 CIT 590. Programming Languages and Techniques Homework 7

Object-Oriented Concepts

CMP 326 Midterm Fall 2015

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Programming Basics. Digital Urban Visualization. People as Flows. ia

CS 101 Spring 2007 Midterm 2 Name: ID:

Computational Expression

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

A Fraction Class. Using a Fraction class, we can compute the above as: Assignment Objectives

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Prof. Navrati Saxena TA: Rochak Sachan

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

CMPS 12A Introduction to Programming Midterm 2 Review Problems

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

University of Massachusetts Amherst, Electrical and Computer Engineering

Key Java Simple Data Types

CSIS 10A Assignment 4 SOLUTIONS

COE428 Lab 4: State Machine

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CS 231 Data Structures and Algorithms, Fall 2016

COE318 Lecture Notes Week 3 (Week of Sept 15, 2014)

University of Palestine. Mid Exam Total Grade: 100

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

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

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

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Chapter 12: Inheritance

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

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

CS 101 Fall 2006 Midterm 3 Name: ID:

Advanced Object Oriented Programming EECS2030Z

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

COE318 Lecture Notes Week 3 (Sept 19, 2011)

CIS 120 Programming Languages and Techniques. Midterm II. November 12, Answer key

CS 101 Exam 2 Spring Id Name

Java Programming for Selenium

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

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

JAVA PROGRAMMING (340)

CSCI 355 LAB #2 Spring 2004

Exercise 0.1 (Review from lectures no submission required)

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Guessing Game with Objects

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

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

Eng. Mohammed S. Abdualal

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

SSE3052: Embedded Systems Practice

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

1.00 Lecture 5. Objects

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture 12: Classes II

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

Object Oriented Programming

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

Final CSE 131 Fall 2015

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

Exercise Sheet 1 - Solutions

Operators and Expressions

CS 302 Week 9. Jim Williams

Java Memory Management

Instance Members and Static Members

Assignment Marking Criteria

Instance Method Development Demo

CSCI 355 Lab #2 Spring 2007

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Software and Programming 1

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

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

CT 229 Methods Continued

Transcription:

Objectives Overview coe318 Lab 2 objects Implement a class. Learn how immutable objects work. Week of September 15, 2014 Create a project with more than one class. Duration: one week. In mathematics, complex numbers combine two real numbers and can be thought of as specifying a point on a plane. The most common ways to express the two components of the complex number are: rectangular: where the two numbers represent the x- and y-components of the point; polar: where one number represents the distance between the origin and the point and the other represents the angle between the x-axis and the line connecting the origin and the point. In this lab, we only consider the rectangular version. The Design of The design of a class means specifying all of its public members. Usually, only some methods and constructors are public (and hence designed); it is highly unusual for instance variables to be public. In Java, a design is expressed in javadocs: specially formatted comments in the source code that describe the API for the class. You are given the design for the class below. The design specifies methods such as getx(), add( z), etc. The source code can be more conveniently accessed here The source code provided consists mainly of method stubs: methods that compile but produce dummy results. A notable exception is the method tostring(); this method, which gives a String representation of a complex number does work and should not be modified. (This method is implicitly invoked in the testing class that produces output.) The main objective of the lab requires that you fix the method stubs so that the program works..java source code Page 1 of 5 package coe318.lab2; * models a complex number expressed * in rectangular form (real and imaginary parts).

* It is an <em>immutable</em> object. * * @author Your Name public class { //Instance variable declarations * Construct a given its * real and imaginary parts. * @param re The real component * @param im The imaginary component public (double re, double im) { //Initialize the instance variables * Returns the real component. * @return the real public double getreal() { return 0.0; //A stub: to be fixed * Returns the imaginary component. * @return the imaginary public double getimaginary() { return 0.0; //A stub: to be fixed * Returns a new number that is * the negative of <em>this</em>. Note: the * original is <b>not</b> * modified. * @return -this public negate() { * Returns a new that is the * sum of <em>this</em> and <em>z</em>. * @return this + z public add( z) { * Returns a new that is the * difference of <em>this</em> and <em>z</em>. Page 2 of 5

* @return this + z public subtract( z) { * Returns a new that is the * product of <em>this</em> and <em>z</em>. * @return this * z public multiply( z) { * Returns a new that is * the reciprocal of <em>this</em>. * @return 1.0 / this public reciprocal() { * Returns a new that is * <em>this</em> divided by <em>z</em>. * @return this / z public divide( z) { * Returns a String representation of * <em>this</em> in the format: * real +-(optional) i imaginary * If the imaginary part is zero, only the * real part is converted to a String. * A "+" or "-" is placed between the real * and imaginary parts depending on the * the sign of the imagrinary part. * <p> * Examples: *..println(new (0,0); -> "0.0" Page 3 of 5

*..println(new (1,1); -> "1.0 + i1.0" *..println(new (1,-1); -> "1.0 - i1.0" * @return the String representation. @Override public String tostring() { //DO NOT MODIFY THIS CODE! String str = "" + this.getreal(); if (this.getimaginary() == 0.0) { return str; if (this.getimaginary() > 0) { return str + " + i" + this.getimaginary(); else { return str + " - i" + -this.getimaginary(); ComplexTry.java source code You are also provided with another class, ComplexTry, which includes a main method that you can use to test your implementation of.java. The source code can be accessed more conveniently here package coe318.lab2; * Do <b>not</b> modify this class. * Its output will be used as part * of the marking procedure for this lab. * @author kclowes public class ComplexTry { * The output should be: * a = 1.0 + i2.0 * b (-a) = -1.0 - i2.0 * c = 2.0 + i3.0 * d (a + c) = 3.0 + i5.0 * e (a - c) = -1.0 - i1.0 * f (1 / a) = 0.2 - i0.4 * g (a / c) = 0.6153846153846154 + i0.07692307692307693 * h (a * c) = -4.0 + i7.0 * @param args Command line args not used. public static void main(string[] args) { a, b, c, d, e, f, g, h; a = new (1, 2); System.out.println("a = " + a); b = a.negate(); System.out.println("b (-a) = " + b); c = new (2, 3); System.out.println("c = " + c); Page 4 of 5

d = a.add(c); System.out.println("d (a + c) = " + d); e = a.subtract(c); System.out.println("e (a - c) = " + e); f = a.reciprocal(); System.out.println("f (1 / a) = " + f); g = a.divide(c); System.out.println("g (a / c) = " + g); h = a.multiply(c); System.out.println("h (a * c) = " + h); Step 1: Create a Netbeans project 1. Create a Netbeans project called Lab2. 2. Create a Java file (class library type) called specifying the package as coe318.lab2 and copy and paste the provided source code. 3. Similarly, create the Java file ComplexTry. Ensure that Netbeans sets the package to coe318.lab2. 4. Generate the javadocs and compile and run the project. 5. It should compile correctly and produce output. Unfortunately, the output is incorrect and you have to fix it. Step 2: Add instance variables and fix constructor and getters 1. Add instance variables for the two components of a complex number. 2. Modify the constructor so that they are properly initialized. 3. Fix the getreal() and getimaginary() methods so that they return the appropriate component. 4. Compile and run your project. The statement System.out.println("a = " + a) should now produce the correct output. Step 3: Fix remaining methods 1. Fix the remaining methods. 2. Suggestion: fix them in the order they are used in ComplexTry. 3. Hint: subtract() and divide() are easier to write by using previously fixed methods. Step 4: Submit your lab 1. Submit your lab by zipping it to a file called lab2.zip 2. Then use the command submit coe318 lab2 lab2.zip to complete the submission. Page 5 of 5