JAVA Methods | Programming

JAVA |
Question 1 (25 marks)(a) (Computing x) Approximate the value of a quantity x using the following series:x = 1 + 1/2 + 1/3 + 1/4+….+1/n where n is a positive integer. The value of n should be passed to the method.Write a java method that calculates and displays the value of x for n (for example n = 1000). [7 marks](b) (Area of regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is where n is the number of sides and s is the side of the polygon. Write a Java method that returns the area of a regular polygon using the following header: public static double area(int n, double side).(Hints: You may need Math.tan() function and Math.PI for area calculations.)  [9 marks](c) (Rectangle class) Design a class to represent a rectangle. The class is specified as follows: (i) Two double data fields named width and height hold the width and height of the rectangle. The default values are 1 for both width and height.(ii) (iii) (iv)A no-arg constructor creates a default rectangle.Getter and setter methods are provided for all data fields. Accessor methods return the area and the perimeter.Question 2 (25 marks)(a) Write the code for an Employee class, which has the following fields: private String name; private double salary;Your class should contain the following:(i) Provide a constructor to set the employee’s name using a passed argument and also to set the salary to $0.(ii) Provide a constructor that accepts arguments to set the employee’s name and salary. The salary should be set using the mutator method for salary described below.(iii) Provide a mutator (i.e. setter) method for the salary that ensures the salary remains within the inclusive range $0 to $100,000. If an attempt is made to set a salary outside this range the current salary will not be changed.(iv) Provide accessor (i.e. getter) methods for the name and for the salary.  [9 marks]  (b) Write a program, called TestEmployee.java, to do the following:(i) Create an employee object referenced by emp1, using the name “Fred”.(ii) Assign a salary of $45,000 for employee “Fred”.(iii) Create an employee object referenced by emp2, using the name “Sue” and assigns her a salary of $55,000, all in the one statement.(iv) On a separate line for each employee, display the employee’s name and 40% of the salary, for taxation purposes.Question 3 (25 marks)The following incomplete WordList class is intended as a collection class for holding a list of words. class WordList { private static final int DEFAULTSIZE = 100; private int maxWords; private String[] list; private int length; }Here the constant provides a default value to replace any request for an illegal array size, the array provides a place to store the words, maxWords will record the size of the array, and length will record how many words are currently stored in it.(a) Write a constructer for WordList that accepts a parameter for the size of the array. The constructor will create an array for the words of the requested size, or of the default size if the size requested is not appropriate. It will also record the size of the array and set the current length appropriately. (5 marks)(b) Write a method addWord to add a word to the array. This will accept a String reference and return a Boolean value indicating whether or not the word in the String was able to be added. It should update the state of the collection object as appropriate. Note that a String object is immutable and can be added safely to the collection without needing to be copied. (10 marks)(c) Write a method findWord to find the first occurrence in the collection of a word beginning with a specified prefix. This method will accept a reference to a String object containing the prefix and return a String reference to the word found, or null if there is no matching word. The String class contains a startsWith instance method, which accepts a String argument and returns a boolean indicating whether the instance string starts with the argument string.

Assignment status: Already Solved By Our Experts

(USA, AUS, UK & CA  PhD. Writers)