Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA PLEASE DEFINE THE FOLLOWING METHODS. Please comment thought process 1. Rationale Enumerations are a useful programmatic tool for modeling a fixed set of

IN JAVA PLEASE DEFINE THE FOLLOWING METHODS. Please comment thought process

1. Rationale

Enumerations are a useful programmatic tool for modeling a fixed set of entities. Java's enumerations behave much as classes and can provide rich functionality through methods.

Command line arguments are an essential means to communicate information to a program at startup. Command line args increase the flexibility of what can be accomplished with a program. This lab is designed to construct a reasonably rich enumeration and use it to conveniently handle conversion of command line arguments to produce nicely formatted output.

2. Quadrants

image text in transcribed

Recall from your days in math class that the 2-dimensional Cartesian plan is divided into four quadrants. The sign of an (x,y) pair dictates which quadrant it will lie in.

Quadrant 1 (Q1) contains coordinates with both x and y positive (+,+)

Quadrant 2 (Q2) contains coordinates with x negative and y positive (-,+)

Quadrant 3 (Q3) contains coordinates with both x and y negative (-,-)

Quadrant 4 (Q4) contains coordinates with x positive and y negative (+,-)

We will model the four quadrants with a couple of Java enumerations, representing negative/zero/positive values, and the four quadrants (plus a "none" quadrant, when the coordinate is on either or both axes). The Quadrant enumeration will have a main() method that takes command line arguments that are pairs of numbers and prints which quadrant to which the pair belongs.

3. Signum enumeration

Values

A Signum represents one of three possible states: NEG, ZERO, POS. Represent these three values and no others.

Fields/Constructors

none required, but you are welcome to add some if you see fit.

Methods

public String toString(). Returns one of these strings as appropriate: "+", "0", "-". Hint: using a switch statement on this is a common useful trick when writing methods over enumerations.

4. Quadrant enumeration

Values

There are five elements of the Quadrant enumeration: Q1, Q2, Q3, Q4, NONE. Ensure that these are all present and no other exist.

Fields

Each Quadrant needs to know if its x and y components are positive, zero, or negative. Use your Signum type internally (private field); we can' check this directly, but you'll need this exact information for the methods below.

Methods

private Quadrant(Signum xSig, Signum ySig). We can't test your private constructor, but use this chance to fill in your private fields. Note: the NONE value should represent all coordinates on either/both axes; store null as its xSig and ySig values.

public boolean xPositive(). Does this quadrant have a positive x?

public boolean yPositive(). Does this quadrant have a negative x?

public String signPair(). Return a string of the format "(+,-)", which shows the String representations of the xSig and ySig fields (as shown below):

(+,+) for Q1

(-,+) for Q2

(-,-) for Q3

(+,-) for Q4

(?,?) for NONE

public Quadrant flipX(). Returns the Quadrant whose xSig has the opposite sign. (NONE is its own x-flipped value).

public Quadrant flipY(). Returns the Quadrant whose ySig has the opposite sign. (NONE is its own y-flipped value).

public static Quadrant fromInts(int x, int y). Given two integers, determine and return the appropriate Quadrant.

public static void main(String[] args).

Accept an arbitrary number of command line arguments.

Treat adjacent arguments as pairs of integers. In processing the arguments, you will be looking at them two at a time.

For each pair of integers, generate the Quadrant to which the pair belongs.

Print the quadrant in which the pair resides along with the signPair(), as in this example format:

(1,5) has signs (+,+) and is in Q1 

Hint: the Integer.parseInt() method is useful; give it a try!

Main Examples

You can both invoke your program on the command line with arguments, or you can also invoke it in the Interactions Pane of DrJava the exact same way (also via the java command, not via the run command):

> java Quadrant // Odd number of args, ignore last > java Quadrant -1 // Valid pair, produce output > java Quadrant 1 5 (1,5) has signs (+,+) and is in Q1 // Output for valid pair, ignore last odd arg > java Quadrant -2 -13 4 (-2,-13) has signs (-,-) and is in Q3 // Handle multiple pairs > java Quadrant 2 -13 4 12 -1 113 19 (2,-13) has signs (+,-) and is in Q4 (4,12) has signs (+,+) and is in Q1 (-1,113) has signs (-,+) and is in Q2 
10 T Il P(3,5) 10 10 -5 (0,0) origin IV -10 T

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions