0% found this document useful (0 votes)
13 views5 pages

Java Function Implementation Guide

The document contains a series of programming tasks related to Java functions, including defining functions with specific parameters, return types, and void functions. It also includes exercises on calculating values based on user input, such as total costs and middle values of integers. Additionally, there is a bonus task involving the calculation of the inner product of two vectors.

Uploaded by

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

Java Function Implementation Guide

The document contains a series of programming tasks related to Java functions, including defining functions with specific parameters, return types, and void functions. It also includes exercises on calculating values based on user input, such as total costs and middle values of integers. Additionally, there is a bonus task involving the calculation of the inner product of two vectors.

Uploaded by

zerapark06531
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PartA

A. Use the function header below to answer questions a) through f):

public static String myFunc (String name, int age, int height) {
< .. insert code here... >
}

(1 mark each —a to f)

a. What is the name of this function?

b. Is this function a void function? (yes or no)


c. What is the return type of this function?
d. How many parameter(s) are there?

e. Name the parameter(s):

f. What is wrong with the snippet of code below?

[Link](myFunc(“Bob”, 16, 172.5));

B. Explain in your own words the main difference(s) between return and void type
functions.
When would you use one over the other? (2 marks)

End of Part A
(space left intentionally blank)

Part B

Do the following:
a) Complete the void function function] that has a String parameter called name and
prints the sentence: (3 marks)
“Hello there, <name>.”

b) Inthe main method, call function using the variable userName. (1 mark)

import [Link];
import [Link];

public class Questionl {


public static void main (String[] args) {
Scanner key = new Scanner ([Link]);
Random r = new Random();
[Link](“Name: ”);
userName = [Link]();

// implement part b) here

public static _ functionl ( ) {

Write the following functions: (2 marks each)


a) The function isOdd() that has an integer parameter num and returns true if numis an
odd number and false if otherwise.

public static isEven ( ){

b) The function divByEleven () that has an integer parameter num and returns t rue if
num is divisible by 11 and false if otherwise.

public static divBySeven ( ){

Do the following:
a) Complete the function function3 that has three integer parameters called nickels,
dimes, and quarters and returns the numeric dollar amount of those number of coins
added together. (3 marks)

b) Inthe main method, call and print function3 using the variables n, d, and q. (1 mark)

Note:

e Nickels are worth 0.05, dimes are work 0.10, and quarters are worth 0.25 of a dollar.
e Example: 3 nickels, 2 dimes, and 4 quarters is worth 1.35

import [Link];
import [Link];

public class Question3 {


public static void main (String[] args) {

Scanner key = new Scanner ([Link]);


Random r = new Random();

[Link](“Nickels: ");
n = [Link]();

[Link](“Dimes: ”);
d = [Link]();

[Link](“Quarters: ");

q = [Link]();

// implement part b) here

public static function3 ( ) {

4. Do the following:
a) Complete the function function4 that has three parameters the foodCost,
tipPercent, and taxPercent, then outputs the numeric total bill amount.

Be sure to:

e Apply the tip to the food cost only and the tax to the food cost only.
e Example: The total cost of a $100 meal with 15% tip and 10% tax would have the
value $100 + $15 + $10 = $125.
b) Inthe main method, call and print function4 using the variables fc, tp, and txp.

import [Link];
import [Link];

public class Question3 {

public static void main (String[] args) {

Scanner key = new Scanner ([Link]);

Random r = new Random();

[Link](“Food Cost: ”);


fc = [Link]();

[Link](“tip:
tp = [Link]();

[Link](“tax:
txp = [Link]();

// implement part b) here

public static

function4

"a

5. Write a function called function5 () that has 3 integer parameters num1, num2, and
num3.
Code the function so that it returns the middle value of the parameters. (4 marks)
o For example:
e The middle value between 5, 2,10 is 5.
e The middle value between 1, 2, 3 is 2.
e The middle value between 15,10,5is 10.
e The middle value between 90, 30, 60 is 60.

public static function5 ( ) {

}
Bonus (2 marks)
The Inner Product between two n-dimensional vectors ¥ = [X1,X2,...,X,] and ¥ = [y1,Y2, +,
Yn] is
defined as:

HY = Xp + XQV2 ++ Xn

Write a function inProd() with two integer array parameters representing n-dimensional
vectors and returns their inner product.

public static inProd ( ){

You might also like