首页 > > 详细

program讲解辅导、辅导Java语言编程

HOMEWORK SPRING 2022
HOMEWORK 1 - due Friday, March 11th no later than 7:00PM (China Time)
Be sure your code follows the coding style for CSE214.
Make sure you read the warnings about academic dishonesty. Remember, all work you submit for homework assignments MUST be entirely your own work. Also, group efforts are not allowed.
Login to your grading account and click "Submit Assignment" to upload and submit your assignment.
You are not allowed to use ArrayList, Vector or any other Java API Data Structure classes to implement this assignment except where noted.
You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.

Vectors are mathematical structures that arise in many different fields of mathematics and computer science, ranging from scientific simulations that require three-dimensional physics to computer graphics, where vectors are used to determine the way light is reflected off of a surface. A vector can be thought of as a sequence of values, referred to as elements in this assignment, that specify the magnitude of the vector in a number of different dimensions. For example, any vector drawn on the Cartesian plane can be thought of as an arrow drawn from the origin to a point (x, y) with its head at the point (x, y). In this case, the x element of the vector is the signed horizontal distance from the origin while the y element is the signed vertical distance from the origin. This can be further extended into three dimensions where another element z is required to specify depth (which in the case of a Cartesian plane would be signed distance into and out of the page). Vectors can have, by mathematical definition, any number of elements greater or equal to one. Any given vector can have duplicate elements and all elements, including leading and trailing zeroes, are significant. The order of elements in a vector is important.

The purpose of this assignment is to design a Vector Abstract Data Type that defines these vectors as Java objects and can have various vector operations performed upon them. A test program will also be required to verify the correctness of the Vector ADT.

1. Write a fully-documented class named VectorADT that stores a vector in an integer array. You may store the elements any way you wish in this array, but it is strongly recommended that the first element is stored in array position zero, the second in position one, etc. all the way up through element n being stored in position n-1. The number of elements in each vector should be declared in a member variable of the VectorADT class. Make sure in your code that you check that the size is less than 60, which should be stored in a final variable called MAX_ELEMENTS. If you don’t use MAX_ELEMENTS and instead use the number 60 the result will be a 5 point deduction! The class should be defined based on the following specification:

public class VectorADT

The VectorADT class implements an abstract data type for vectors that contains common vector operations.

Constructor for VectorADT
public VectorADT(int size)

Construct an instance of the VectorADT class of a certain size with all elements set to 0.

Parameter:

size - the size that this VectorADT will be

Precondition:

size is between 1 and MAX_ELEMENTS, inclusive.

Postcondition:

This VectorADT has been set to the zero vector (all elements are zero) of with a size equal to the parameter.

Throws:

InvalidSizeException - Indicates that size is not within the valid range.

clone
public Object clone()

Generates a copy of this VectorADT.

Returns:

The return value is a copy of this VectorADT. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be typecast to a VectorADT before it can be used.

equals
public boolean equals (Object obj)

Compare this VectorADT to another object for equality.

Parameters:

obj - an object to which this VectorADT is compared

Returns:

A return value of true indicates that obj refers to a VectorADT object with the same values and size as this VectorADT. Otherwise, the return value is false.

Note:

If obj is null or it is not a VectorADT object, then the return value is false.

setElement
public void setElement(int value, int element)

Sets the element associated with the given position for this VectorADT.

Parameters:

value - new value for the element in this VectorADT

element - position of the element to alter in this VectorADT

Preconditions:

This object has been instantiated and element is a valid element (between 0 and the size of this VectorADT - 1, inclusive).

Postcondition:

Sets the value of the given element in this VectorADT to the given value.

Throws:

IllegalArgument- Indicates that element does not correspond to a valid position of the array.

getElement
public int getElement(int element)

Gets the element associated with the given position for this VectorADT.

Parameter:

element - position of the element to access in this VectorADT

Preconditions:

This object has been instantiated and element is a valid element (between 0 and the size of this VectorADT - 1, inclusive).

Returns:

The value of the given element in this VectorADT.

Throws:

IllegalArgument- Indicates that element does not correspond to a valid position of the array.

getSize
public int getSize()

Returns the size of this VectorADT.

Returns:

The size of this VectorADT object.

add
public static VectorADT add(VectorADT v1, VectorADT v2)

Generates and returns the sum of two given VectorADTs. Note that vector addition is defined by adding the corresponding elements of each vector to get the corresponding element of the sum vector.

Parameters:

v1 - the first VectorADT

v2 - the second VectorADT

Preconditions:

The VectorADT objects referred to by v1 and v2 have been instantiated and both have the same size.

Returns:

A VectorADT containing the sum of the two given VectorADT parameters (v1 + v2).

Throws:

IllegalArgument- Indicates that either v1 or v2 is null.

InvalidSizeException- Indicates that v1 and v2 are of different sizes.

subtract
public static VectorADT subtract(VectorADT v1, VectorADT v2)

Generates and returns the difference of two given VectorADTs. Note that vector subtraction is defined by subtracting the corresponding elements of each vector to get the corresponding element of the difference vector.

Parameters:

v1 - the first VectorADT

v2 - the second VectorADT

Preconditions:

The VectorADT objects referred to by v1 and v2 have been instantiated and both have the same size.

Returns:

A VectorADT containing the difference of the two given VectorADT parameters (v1 - v2).

Throws:

IllegalArgument- Indicates that either v1 or v2 is null.

InvalidSizeException- Indicates that v1 and v2 are of different sizes.

multiplyByScalar
public static VectorADT multiplyByScalar(VectorADT v, int scalar)

Generates and returns the product of a VectorADTs and a scalar (i.e., regular number). Note that multiplication by a scalar is to multiply each element in the vector by the scalar number.

Parameters:

v - the VectorADT

scalar - the scalar to multiply the VectorADT by

Precondition:

The VectorADT object referred to by vector has been instantiated.

Returns:

A VectorADT containing the product of the VectorADT and the scalar.

Notes:

1. The return value is null if v is null.
2. The object being referred to by v is unaltered.
3. Cloning v before multiplying is a requirement in this method or a 5 point deduction will occur. It is also recommended that you write a non-static helper method that allows all elements in a vector to be multiplied by a scalar number.

dotProduct
public static int dotProduct(VectorADT v1, VectorADT v2)

Generates and returns the dot product of two given VectorADTs. Note that the dot product is defined as the sum of all the products of corresponding elements in the two vectors, shown as follows:

(v1[0] * v2[0]) + (v1[1] * v2[1]) + … + (v1[v1.getSize() - 1] * v2[v2.getSize() - 1])

Parameters:

v1 - the first VectorADT

v2 - the second VectorADT

Preconditions:

The VectorADT objects referred to by v1 and v2 have been instantiated and both have the same size.

Returns:

The dot product of the two given VectorADT parameters.

Throws:

IllegalArgument- Indicates that either v1 or v2 is null.

InvalidSizeException- Indicates that v1 and v2 are of different sizes.

toString
public String toString()

Returns a String representation of this VectorADT.

Returns:

The elements of this VectorADT in order from element 0 to element getSize() - 1, each separated by spaces.

Note:

You are not allowed to use any Java API classes to implement VectorADT.
You may define additional methods as you wish as long as all of the above methods meet the given specifications.
2. Write a fully documented class named VectorOperations that is based upon the following specification:

public class VectorOperations
The VectorOperations class is a Java application that tests the methods of the VectorADT class and allows the user to input long vectors as a list of elements separated by spaces and perform operations on them.

main
public static void main(String[] args)
When the program begins, the main program should display a menu that allows following operations:
A (Add two vectors)
D (Dot product)
E (test if two vectors are equal)
M (Multiply two vectors)
S (Subtract two vectors)
Q (Quit Program)


3. Also, provide additional class(es) to handle the exceptions.

Note: You may include additional methods in the VectorADT class or VectorOperations, as necessary.

INPUT FORMAT:

Each vector is entered on one line.
Menu options are case-insensitive (e.g. 'A' and 'a' are the same). You may use the equalsIgnoreCase method of the String class for these comparisons.
Each vector is to be entered on one line, with each element to be separated by spaces. The first element entered is the first element of the vector. ORDER IS IMPORTANT. The size of the VectorADT to be created from a line is equal to the number of input values on that line (HINT: The StringTokenizer class has a countTokens() method that might be useful).
You may assume that all input numbers are integers (negative or positive).
YOU ARE NOT REQUIRED TO CHECK FOR INVALID NUMBERS IN THE INPUT. Your program will not be tested with invalid numbers. However, VectorADTs with different sizes will be checked.
OUTPUT FORMAT

Echo the elements of the vectors to the user after each input entry has been completed.
Be sure your prompts and error messages for the user are clear and understandable.
Output must be accompanied by additional information (or symbols) explaining the specific operation, as shown in the sample input/output.
SAMPLE INPUT/OUTPUT


Note: Program output is in blue, comments are in green and user input is in black.

A) Add
D) Dot Product
E) Equality
M) Multiply
S) Subtract
Q) Quit program



Enter Your Choice: A


Enter First Vector:
1 2 3 4 5 16 7 8


Enter Second Vector:
8 7 6 5 4 3 2 1


1 2 3 4 5 16 7 8 +

8 7 6 5 4 3 2 1 =

9 9 9 9 9 19 9 9


// Menu not shown
Enter Your Choice: a // Ignores case

Enter First Vector:
0 0 0 0 0 0 0 -3 3 5 6 7



Enter Second Vector:
3 4 6


0 0 0 0 0 0 0 -3 3 5 6 7 +

3 4 6 =

Size Error



// Menu not shown
Enter Your Choice: S

Enter First Vector:
9 8 7 6 5 4 3 2 1


Enter Second Vector:
1 2 3 4 5 6 7 8 9



9 8 7 6 5 4 3 2 1 -
1 2 3 4 5 6 7 8 9 =
8 6 4 2 0 -2 -4 -6 -8


// Menu not shown
Enter Your Choice: S

Enter First Vector:
4 3 2 5 1 3 7 20


Enter Second Vector:
2 5 7 9 2 3 5 8


4 3 2 5 1 3 7 20 -
2 5 7 9 2 3 5 8 =
2 -2 -5 -4 -1 0 2 12


// Menu not shown
Enter Your Choice: M

Enter Number:
5



Enter Vector:
1 5 7 2 4 -7


5 *
1 5 7 2 4 -7 =
5 25 35 10 20 -35


// Menu not shown
Enter Your Choice: M

Enter Number:
0


Enter Vector:
1 2 5 -6 0 3 2 5 0 -1 2 4 5 12 4


0 *
1 2 5 -6 0 3 2 5 0 -1 2 4 5 12 4 =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


// Menu not shown
Enter Your Choice: d // Ignores case

Enter First Vector:
1 2 6 8 3 4 8


Enter Second Vector:
2 4 7 4 2 4 0


1 2 6 8 3 4 8 (dot)
2 4 7 4 2 4 0 =
106

// Menu not shown
Enter Your Choice: D

Enter First Vector:
1 1 1 1 1


Enter Second Vector:
2 3 4 5 6


1 1 1 1 1 (dot)

2 3 4 5 6 =

20



// Menu not shown
Enter Your Choice: E

Enter First Vector:
1 2 4 6 7


Enter Second Vector:
1 2 4 6 7



1 2 4 6 7 =

1 2 4 6 7 ?

true



// Menu not shown
Enter Your Choice: E

Enter First Vector:
1
Program terminating normally...

Course Info | Schedule | Sections | Announcements | Homework | Exams | Help/FAQ | Grades | HOME

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!