Java | JSP | JSF Answers

Questions: 4 418

Answers by our Experts: 3 943

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search & Filtering

a. Write a Java program to compute the area of a polygon.

Area of a polygon = (n*s^2)/(4*tan(π/n))

where n is n-sided polygon and s is the length of a side

Input Data:

Input the number of sides on the polygon: 7

Input the length of one of the sides: 6

b. Explain each of the following Java programming terms

i. extended [1]

ii. protected [1]

iii. instanceOf [1]

iv. abstract [1]

v. Write a Java program to print the contents of a two-dimensional Boolean array where t will represent true and f will represent false.


1.  Identify two instance variables and two methods from the case below.

An application that manipulates Points on a plane is to be designed using Objected Oriented

Design. In this application, Points must have X and Y coordinates, move on the plane as well as display their current values of X and Y. Points can also determine if they are in the zero position (0,0) with a Boolean answer. [8]

 

a. Assume the existence of a Java file myClass.java. Explain how Java uses both a compiler and interpreter to achieve its architecture-neutral characteristic. [5]

b. Using an example, explain the implication of using the keyword void in a method declaration

c. Given an Object-Oriented System that manages student records (name, date of birth,[3]

d. gender etc), use examples to differentiate between Class and Object as used in OOP.[4]


3) Refer to the code segment below and answer the questions in the comments labelled Question i – vi. For each answer; provide a brief explanation for your answer.


public class Scope {

private int x = 2;

private int x = 1;


public void start() {

int x = 5;

int x = 2;


//Question i

System.out.printf( "The value of x is %d\n", x );

int x = 2; 


//Question ii

System.out.printf( "\nThe value of x is is %d\n", x );

} // end method begin


public void middle() {

int x = 25;


//Question iii

System.out.printf( "\nThe value of x is %d\n", x );

++x; 


//Question iv

System.out.printf( "The value of x is %d\n", x );

} // end method middle


public void end() { 


//Question v

System.out.printf( "\nThe value of x is %d\n", x );

x *= 10; 


//Question vi

System.out.printf( "The value of x is %d\n", x );

} // end method end


} // end class Scope


A. Consider a problem to find the student who had the highest GPA for the 2020/2021 academic year. i. Explain your choice of either a search or a sorting algorithm to solve this problem.


ii. Write the algorithm for your implementation. [


B. Consider the analogy of a pile of books on a table. Using a diagram, explain the basic operations involved in adding a book to the pile and removing a book from the pile.


C. Use the code below to answer the questions that follow:


public class CS204 { public static int linearSearch(int[] data, int target) {


for (int i = 0; i < data; i++) {

if (target == data[i]){ return i; 5

}


}

return -1;


}


public void main(String[] args) {

int[] data = {3, 14, 7, 22, 45, 12, 19, 42, 6};

System.out.println("Search for 7: " +

linearSearch(7));


}

}


i. Why is method linearSearch declared static?

ii. Identify and resolve the errors in the code.

Q3 (B) The performance of an algorithm can be determined in terms of its time and space complexity. Explain the difference between time complexity and space complexity and make a case for which is the most efficient method for determining the performance of an algorithm. [7 marks] 


Question 3


A. Refer to the code segment below and answer the questions in the comments labelled Question i – vi. For each answer; provide a brief explanation for your answer.


public class Scope

{

private int x = 2;

private int x = 1;


public void start()

{


int x = 5;

int x = 2;


//Question i


System.out.printf( "The value of x is %d\n", x );


int x = 2;


//Question ii


System.out.printf( "\nThe value of x is is %d\n", x );

} // end method begin


4 public void middle()

{

int x = 25;


//Question iii


System.out.printf( "\nThe value of x is %d\n", x );

++x;


//Question iv


System.out.printf( "The value of x is %d\n", x );


} // end method middle


public void end()

{


//Question v


System.out.printf( "\nThe value of x is %d\n", x );


x *= 10;


//Question vi


System.out.printf( "The value of x is %d\n", x );

} // end method end


} // end class Scope



A.) A module is required to enable lecturers to easily identify the highest and lowest performing students in their courses. As the lead programmer, write a java program to achieve this. Your program should have the following features: Each correctly implemented feature earns you 3 marks.


i. The program should make use of a sentinel to control the number of grades entered.


ii. Total Number of grades entered


iii. The average of the grades


iv. Number of students that achieved a grade of 80 and above


v. Number of students that achieved a grade of 50 and below


B.) An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute. Write an algorithm for the implementation of the sentinel control in Question A Sub Question I above.


C.) For larger datasets, a linear search may be inadequate or perhaps inefficient. What other search method can be employed to return the maximum number from a set of elements in an array. Explain your answer.


A. Write a java program that reads three (3) int variables and performs the below operations: i. Prints out their product ii. Prints out the maximum of the numbers Do not use any inbuilt Java Methods to return the maximum. Extra marks will be awarded for clarity of code and comments. [8 marks] B. Algorithms perform differently on different data sets and as such we may be interested in either their Best-case, Worst-case or Average-case scenarios. Explain why the Worst-case for an insertion sort is with reverse-sorted data. [6 marks] C. Identify and correct the errors in each of the following sets of code: [6 marks]


i. while ( c <= 5 ) {

product *= c;

++c;


ii. if ( gender == 1 )

System.out.println( "Woman" );

else;

System.out.println( "Man" );


iii. What is wrong with the following while statement?


while ( z >= 0 ) sum += z;


D. Explain two (2) programming situations that will require the use of overloaded methods [5 marks]


(ii)Explain two (2) programming situations that will require the use of overloaded methods [5 marks]


Use the code below to answer the questions that follow:


public class CS204{

public static int linearSearch(int[] data, int target) {

for (int i = 0; i < data; i++) {

if (target == data[i]){

return I;


}

}

return -1;



public void main(String[] args) {

int[] data = {3, 14, 7, 22, 45, 12, 19, 42, 6};

System.out.println("Search for 7: " + linearSearch(7));

}


QUESTIONS


i. Why is method linearSearch declared static?


ii. Identify and resolve the errors in the code.

LATEST TUTORIALS
APPROVED BY CLIENTS