0% found this document useful (0 votes)
30 views3 pages

Name: Program: Studentid: Group: Q1.Write Method Headers (Not The Bodies) For The Following Methods

The document provides instructions and questions for a programming pre-assessment. It includes sample code for writing method headers to calculate sales commission, display a calendar, calculate square root, and calculate monthly loan payments. It also includes questions to identify which method would be invoked given different method signatures and parameter types. Finally, it provides a coding question to write a LengthConverter class with methods to convert between feet and meters, and a main method to test the conversions.

Uploaded by

Syed jibril
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Name: Program: Studentid: Group: Q1.Write Method Headers (Not The Bodies) For The Following Methods

The document provides instructions and questions for a programming pre-assessment. It includes sample code for writing method headers to calculate sales commission, display a calendar, calculate square root, and calculate monthly loan payments. It also includes questions to identify which method would be invoked given different method signatures and parameter types. Finally, it provides a coding question to write a LengthConverter class with methods to convert between feet and meters, and a main method to test the conversions.

Uploaded by

Syed jibril
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Instruction:

Write your answer and submit on time via VLE OOP _ Pre Assessment 5 October 2022

Name : Program:

StudentId: Group:

Q1.Write method headers (not the bodies) for the following methods:

a. Return a sales commission, given the sales amount and the commission rate.

b. Display the calendar for a month, given the month and year.

c. Return a square root of a number.

d. Return the monthly payment, given the loan amount, number of years, and annual interest rate.

public class Q1Method {

// Method header for calculating sales commission

public double calculateSalesCommission(double salesAmount, double commissionRate) {

// Method body

// Method header for displaying a calendar

public void displayCalendar(int month, int year) {

// Method body

// Method header for calculating the square root of a number

public double calculateSquareRoot(double number) {

// Method body

// Method header for calculating monthly payments

public double calculateMonthlyPayment(double loanAmount, int numberOfYears, double


annualInterestRate) {

// Method body

}
Instruction:
Write your answer and submit on time via VLE OOP _ Pre Assessment 5 October 2022

Q2.Given two method definitions,

public static double sum(double x, double y)

public static double sum(int x, double y)

identify and write which of the two methods is invoked for:

a. double z = sum(4, 5); ?

b. double z = sum(4, 5.4); ?

c. double z = sum(4.5, 5.4); ?

a. The method public static double sum(double x, double y) is not invoked for double z = sum(4, 5);
because the arguments 4 and 5 are both integers and do not match the method signature.

b. The method public static double sum(int x, double y) is invoked for double z = sum(4, 5.4);
because the first argument 4 is an integer and the second argument 5.4 is a double. The compiler
performs an implicit conversion of the integer 4 to a double before invoking the method.

c. The method public static double sum(double x, double y) is invoked for double z = sum(4.5, 5.4);
because both arguments 4.5 and 5.4 are doubles and match the method signature.

Q3.Write Java code:


Instruction:
Write your answer and submit on time via VLE OOP _ Pre Assessment 5 October 2022

public class LengthConverter {

// Method to convert feet to meters

public static double footToMeter(double foot) {

double meter = 0.305 * foot; // 1 foot = 0.305 meter

return meter;

// Method to convert meters to feet

public static double meterToFoot(double meter) {

double foot = 3.279 * meter; // 1 meter = 3.279 feet

return foot;

// Main method for testing the footToMeter and meterToFoot methods

public static void main(String[] args) {

System.out.println("Feet\tMeters\t|\tMeters\tFeet"); // print table headers

System.out.println("----------------|------------------"); // print table separator

for (double i = 1.0; i <= 65.0; i++) {

double lengthInMeters = footToMeter(i);

double lengthInFeet = meterToFoot(i);

System.out.println(i + "\t" + String.format("%.3f", lengthInMeters) + "\t|\t" + i + "\t" +


String.format("%.3f", lengthInFeet)); // print formatted table row

You might also like