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

  1. Write a class named Pet, which should have the following fields:
  • name, The name field holds the name of a pet
  • The animal field holds the type of animal that a pet is Example values are”Dog”,”Cat”, and “Bird”.
  • The age field holds the pet’s age.

           The Pet class should also have the following methods:

  • The setName method stores a value in the name field.
  • The setAnimal method stores a value in the animal field
  • The setAge method stores a value in the age field.
  • The getName method returns value of the name field
  • The getAnimal method returns the value of the animal field
  • The getAge method returns the value of the age field
  • implement a toString() method to display the pet name, type and age

2. Write a test class to test your Pet class. Make sure you have three different types of pets. (three Pet objects)


Method overriding to calculate simple interestCreate an abstract class Bank with method calculateSimpleInterest method which takes the amount of type double and time of type int (in years) as parameters and returns double type simple interest with interest at 6 PPA.

 

Create classes BankABankB and BankC which extends class Bank and override the method calculateSimpleInterest. Class BankA, BankB, and BankC calculate the interest at 10 PPA, 9 PPA, and 7 PPA respectively in an overridden method.

 

Input

   1

   45000

   3


   where,

  • First line represents a choice for calling one of the three classes. (1 for BankA, 2 for BankB and 3 for BankC)
  • Second line represents principal amount.
  • Third line represents time in years.

 

 

Output

   13500.00

 

  where the output in 2 decimal numbers.


  1. Write a custom sorting method that rearranges a List of LivingSpace objects so that all the properties that do not have an assigned address are moved to the front of the list, in order of descending number of bedrooms. All properties with an address may be left unsorted at the end of the list.
  2. An insertion sort is probably the best-suited algorithm for this task. Do not implement a selection or merge sort algorithm. Java's built-in sorting methods use a highly-optimized merge sort, so there is rarely a good reason to write your own version. Selection sort is rarely useful when speed is the primary concern Your solution should have a method header that looks like: public static void customSort(List properties);
  3. In your main method, give an example where the sorting algorithm you created in step 1 is faster than a solution using Collections#sort(List, Comparator), and an example where it is slower

Create a Java ArrayList of String type “fromCity”, use “Add()” method to add three elements: “Los Angeles”, “San Francisco”, and “Portland”. Create another Java ArrayList of String type “toCity” with three elements: “Seattle”, “Denver”, and “Chicago”. Make two-dimensional array of int type named “distance” whose elements are from table. Be sure to make the elements using the index of [from][to].

4x4 Table:

fromCity \ toCity | 0 | 1 | 2 | 3

0 |1135 | 1016 | 2015

1 | 807 | 1250 | 2128

2 | 174 | 1240 | 2121


Ask user to enter name of city to “travel from”, use “indexof()” method to find the index of the given city in the “fromCity” ArrayList.

Ask user to enter the name of city to “travel to”, then use“indexOf()” method to find the index of the given city in the “toCity” ArrayList. Find “distance” array and display it.



Write a program that computes the area of a circular region (the shaded area in the diagram), given the radii of the inner and the outer circles, ri and ro, respectively.



Create a Java ArrayList of String type “fromCity”, use “Add()” method to add three elements: “Los Angeles”, “San Francisco”, and “Portland”. Create another Java ArrayList of String type “toCity” with three elements: “Seattle”, “Denver”, and “Chicago”. Make two-dimensional array of int type named “distance” whose elements are from table. Be sure to make the elements using the index of [from][to].

4x4 Table:

fromCity \ toCity | 0 | 1 | 2

0 | 1135 | 1016 | 2015

1 | 807 | 1250 | 2128

2 | 174 | 1240 | 2121


Ask user to enter the name of city to “travel from”, use “indexof()” method to find the index of the given city in the “fromCity” ArrayList.

Ask user to enter the name of city to “travel to”, then use“indexOf()” method to find the index of the given city in the “toCity” ArrayList. Find “distance” array and display it.


a. 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.

B. 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. [5 marks]


I. 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]


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]


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. [7.5 marks]


ii. Write the algorithm for your implementation. [7.5 marks]


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. [5 marks]


C). Use the code below to answer the questions that follow: [5 marks]


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));

}

}


i). Why is method linearSearch declared static? Identify and resolve the errors in the code.

ii) identify nd resolve the errors in the code.


LATEST TUTORIALS
APPROVED BY CLIENTS