留学论文

my first programming course final in university

Question 1[19 points]
i) [10 points] (a) Given the Point class specification on page 8, write a complete class called Triangle. The class has 3 data fields p1, p2, and p3, all of type Point, the 3 points representing the triangle. The class has 3 constructors. The first constructor has 3 arguments of type Point, representing the points of the triangle. The second constructor has 6 double arguments representing the coordinates x, and y of the 3 points. The third constructor constructs a default triangle with the points (0,0), (1,1), and (2,0). The class has 3 inquiry methods, as well as a toString() method. The class also has a single mutation method that changes the coordinates of one of the three points of the triangle depending on the value of an int argument. The int argument has a value between 1 and 3 which represents the point number to change (1 for p1, 2 for p2, or 3 for p3). If the value of the argument is not between 1 and 3, an exception is thrown.
Question 1(Cont.)
ii) [9 points] write a tester class called TriangleTester to test the Triangle class. The class contains a single method main. Six double numbers which represent the x and y coordinates of the 3 points of the triangle are entered on the command line. They are stored in the String array args of the main method. After converting them to double values, you should construct 3 Triangle objects t1, t2, t3, by calling each of the 3 constructors with their appropriate arguments. Then, using one of these objects, display the results returned by all the methods in the Triangle class. Also show in the tester class how to use the getX and getY methods in the Point class to display the coordinates of point p1 of that object. Call also the mutation method to change the coordinates of point p2 to (3.5, 5.8).
Question 2 [10 points]
Write the statements that draw the following graphics. All the graphic objects change size with the size of the window. Both triangles are defined using the GeneralPath Class. The 2 arcs forming the center circle are defined using the Arc2D.Double class. The 2 arcs are defined so that the resulting circle is centered in the window, with diameter equal to half the minimum of the width and height of the window. You do not need to draw the objects. Just fill the lower triangle in pink, the upper triangle in yellow, the left arc in red, and the right arc in blue.
Question 3 [10 points]
Write a nested loop that reads student marks from the keyboard using the Scanner class until a negative mark is entered, then displays the average and asks if the user wants to enter marks for another student. Any answer starting with an “N” terminates the loop. Typical output should look like
Enter marks for a student terminated by a negative mark 65 85 90 -1 The average for this student is 80 Do you want to enter marks for another student [Y/N]? Y Enter marks for a student terminated by a negative mark 55 75 70 -1 The average for thus student is 66.67 Do you want to enter marks for another student [Y/N]? N
Question 4 [9 points]
i) Write a method with prototype
public void drawTriangle(int n)
that draws triangles using asterisks. Here n is the number of rows.
For example, the result for 4 rows is given by
****
***
**
*
ii) Write a recursive method with prototype
public String reverse(String s)
that returns a string that is the reverse of the original string s.
For example the reverse of Help} is pleH.
iii) Write a non-recursive (loop) version of the reverse method defined in ii).
Question 5 [12 points]
i) Write statements to define a two-dimensional array v of type int with 3 rows and 4 columns.
Use nested for loops to assign to each element v[i][j] the value i + j.
ii) Write a method that has as input argument a one-dimensional array of type String which represents bank account owner names. The method constructs and returns a one-dimensional array of type BankAccount with the names as defined in the string array, using account numbers 1, 2, 3, etc… and using an initial balance of 0 for each account.
iii) Write a method that finds the owner name of the account, in a given array of type BankAccount, that comes last in lexicographical order.
Brief Documentation for the Point and the BankAccount Classes
The Point class constructor and method prototypes
// construct a point object with given x, y coordinates public Point(double x, double y) // return the x-coordinate of the point public double getX() // return the y-coordinate of the point public String getY() // return a string representation of a point in form (x,y) public String toString()
The BankAccount class constuctor and method prototypes
// construct account with account number a, owner name n and initial balance b public BankAccount(int a, String n, double b) // withdraw given amount (return true if valid) public boolean withdraw(double amount) // deposit given amount public void deposit(double amount) // get the values of the three data fields public int getNumber() public String getName() public double getBalance()
// construct a Scanner object public Scanner(System.in) // read a line as an integer and return it public int nextInt() // read a line as a double and return it public double nextDouble() // read a line as a string and return it public String nextLine()

1 Comment to “my first programming course final in university”

  1. For that motive that you’ll find a variety of blogs and forums with distinct factors of view, they test your considering. It genuinely is at these moments once you have practical insignt a lot of others may not have had, which contain the blogger herself/himself.

Leave a Reply

名字 域名组合

编程之邦 知识架构