You are on page 1of 3

COM112β - Programming Techniques Department of Computer Science

B.Sc. (General) Degree Program - Level I (2019) University of Ruhuna


Assignment - 06

Methods in Java

1. Write following two java programs that capable to add and subtract given two numbers.

 Methods without passing parameters


class Cal1
{
public static void main(String[] args) {
add();
sub();
}

public static void sub() {


int a = 25;
int b = 10;
int sub = a-b;
System.out.println("Subtraction of two numbers = " + sub);
}

public static void add(){


int a = 25;
int b = 10;
int tot = a+b;
System.out.println("Sumation of two numbers= " + tot);
}
}

 Methods which use parameters


class Cal2
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
add(25,10);
sub(50,20);
}

public static void sub(int a, int b) {


int sub = a-b;
System.out.println("Subtraction of two numbers = " + sub);
}

public static void add(int a, int b){


int tot = a+b;
System.out.println("Sumation of two numbers= " + tot);
}

}
import java.util.*;
class Cal3
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x ,y;

System.out.print("Input number for a :") ;


x = input.nextInt();

System.out.print("Input number for b :") ;


y = input.nextInt();

add(x,y);
sub(x,y);
}

public static void sub(int a, int b) {


int sub = a-b;
System.out.println("Subtraction of two numbers = " + sub);
}

public static void add(int a, int b) {


int tot = a+b;
System.out.println("Sumation of two numbers= " + tot);
}
}

 Methods which return values

import java.util.*;
class Cal4
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x ,y;

System.out.print("Input number for a :") ;


x = input.nextInt();

System.out.print("Input number for b :") ;


y = input.nextInt();

int p = add(x,y);
int q = sub(x,y);

System.out.println("Sumation of two numbers= " + p);


System.out.println("Subtraction of two numbers = " + q);
}

public static int sub(int a, int b) {


int sub = a-b;
return sub;
}

public static int add(int a, int b) {


int tot = a+b;
return tot;
}
}
2. Write a Java program to find the maximum number out of two numbers using a method called max( ).
 Numbers should be input from the keyboard at the main method.
 Output of the program should be produce at the main program.
 Following algorithm can be used to find the maximum at the max() method

When numbers are num1, num2

if (num1 > num2)


result = num1;
else
result = num2;

3. Following array consists with marks of five students.


{75,56,89,45,67}

Write a java program which has two methods call findAverage() and findMax() to get the average mark and the
maximum mark out of all marks.
 Array should be define in the main method and output should be produced at the main method.
 Following algorithm can be used to find the maximum at the findMax() method

int max=m[0];
for (int i = 0; i < m.length; i++) {
if (m[i] > max)
max = m[i];
}

4. Write a java program to find circumference and area of a circle. Your program should be fulfill following
requirements.
 Radius of the circle should be read in the main method of the program
 Circumference and Area should be calculated at the circumference() and area() methods.
 Circumference and Area should be displayed at the main method.

5. Write a Java program to store rainfall figures for 30 days for the month of June. You should save the
numbers in an array data structure.
a. Display the average rainfall figure for the month.
b. Display the sorted rainfall figures. (Use Arrays.sort(name of array))

You might also like