top of page

Object Oriented programming With JAVA | Codersarts

Updated: Mar 25, 2021


Task: 1


The mathematician Euler proved the following:

(𝜋 /6)^2 = 1 + 1 /4 + 1/ 9 + 1/ 16 + ⋯


Write a recursive Java method that keeps calling itself until the sum is close to (𝜋 /6)^2 (i.e when then sum has an error less than 0.001) then it prints the number of terms added to verify that the above equation is true. Show the output of a call to this function.


Task: 2


The Caesar Cipher technique is one of the earliest and simplest method of encryption technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C, and so on. Thus to cipher a given text we need an integer value, known as shift which indicates the number of position each letter of the text has been moved down.


The encryption can be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a shift n can be described mathematically as,


𝐸(𝑥) = (𝑥 + 𝑛) 𝑚𝑜𝑑 26


a) Complete the following JavaFx program to produce the scene below.


Label lbl1 = new Label("Plain text");
 TextField plain = new TextField();
 Label lbl2 = new Label("Ceaser Shift");
 TextField shift = new TextField();
 Label lbl3 = new Label("Cipher text");
 TextField cipher = new TextField();

 Button encryBtn = new Button("Encrypt");

 /* add code here */
 …………………………………………………………………………….
 Scene scene = new Scene(p1);
 stage.setTitle("Encryption");
 stage.setScene(scene);
 stage.show();

 public static void main(String[] args) {
 launch(args);
 }
}
}

b) Clicking the “Encrypt” button generates the cipher text. Implement button setOnAction method for this button to perform the correct action on button click. Assume the input plain text will be string of upper case letters and the required shift will be an integer between 0-25. (4 marks)


Task: 3

Several design properties for a problem are presented below. Use these properties in order to write all the necessary classes and/or interfaces for a solution to the problem. Focus on class structure and interaction. You may implement your solution however you wish, but you will be graded on the appropriateness of your solution to the requirements. Note the use of capitalization and parentheses for clarification. You may use whatever constructors or additional methods you wish. Finally, let the specific behavior for methods is just printing appropriate messages whenever needed.


You must define a structure that can represent Bottles. It comes with different wide, tall and contents. It can be filled() with liquid content that need to be packaged. Bottles for various uses are typically made of a specific kind of Plastic or Glass. Glass used for bottles is of two kinds – Soda-Lime glass and Lead Crystal glass. Soda-Lime glass is commonly used for all bottles while Lead Crystal glass may be used for some premium fragrances. Finally, any Glass bottles can be recycled(). Drink cans can also be recycled(). But, not all Plastic bottles can be recycled(). Create a reference of type recycled material and assign to it a lead crystal perfume bottle of 4 inches wide and 6.5 inches tall.


Task: 4

A fast-food restaurant operates both a drive-through facility and a walk-in facility to serve customer orders. Each customer order receives a receipt which has a unique number, date/time, items total, tax percent and a total to pay. The restaurant uses colored thermal paper rolls to set the customers apart from others. Green color is used for printing walk-in customers’ receipts and white receipts for drive-through customers. In order to reduce greenhouse gases, the walk-in customer is granted a discount percent on his/her order receipt.


a) Design and implement Java classes to represent receipt and its two types; walk-in and drivethrough. Each class is comparable with itself in order to sort receipts by their number.

b) In each class, implement constructors and appropriate accessor and mutator methods.

c) Define a method to compute the total to pay based on items total and tax percent and discount percent(if any). The formula to compute the total to pay without discount is:

Total to pay = items total (1+tax ratio)


When there is a discount, the formula is:


d) Redefine the method toString in each class to concatenate all fields in each class and return it as string. The returned String should also indicate the receipt colour for green and white receipts. (4.5 marks)


e) Redefine the method equals in each class to check if instance objects are of the same class or not. (4.5 marks)


f) Design and implement a test class. This should contain a main method to do the following:


  • Fill in a list of receipts with hard-coded test data (no user input). The receipts list should hold receipt objects of any type. (4 marks)

  • Using Java Collections.sort() method, display a sort list of receipts by their number. (2 marks)

  • Using Java Collections.frequency() method, display number of green receipts and the number of white receipts. (2 marks)

  • Define a method that adds receipts information to a new map structure that associates each date with a list of the receipts that have been issued on the same date. Then for each date, it finds and prints the total sales of green receipts and the total of while receipts. Finally, make a call to this method to show results. (16 marks)


Task 5:

The owner of a photocopy store charges 5 cents per copy. In addition there is a setup fee for 2.5$ for each photocopying job. The store revenue from selling x copies is:


𝑟𝑒𝑣𝑒𝑛𝑢𝑒 = 0.05𝑥 + 2.5


If it costs the owner 3 cents per copy, write a Java program that reads a text file which contains the sales of each day during a period of time then determines the total revenue, cost and profit over this period of time. (Profit = revenue-cost). (Put your Java code inside a try—catch block and don’t forget to import the required library).



Comments


bottom of page