Objective:
- Methods in Java
Activity 1:
Write Java program
that will ask the user to enter the width and length of a rectangle and then display
the rectangle’s area. The program calls the following functions, which have not
been written:
- getLength: This
function should ask the user to enter the rectangle’s length and then return
that value as a double .
- getWidth: This
function should ask the user to enter the rectangle’s width and then return
that value as a double .
- getArea: This
function should accept the rectangle’s length and width as arguments and return
the rectangle’s area. The area is calculated by multiplying the length by the
width.
- displayData: This function should accept the rectangle’s area as argument and display it in an appropriate message on the screen.
Solution:
import java.util.Scanner;
public class Main {
static double getLength() {
double length;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Length of Rectangle: ");
length = scanner.nextDouble();
return length;
}
static double getWidth() {
double width;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Width of Rectangle: ");
width = scanner.nextDouble();
return width;
}
static double getArea(double i, double j) {
double area;
area = i * j;
return area;
}
static void displayData(double area) {
System.out.println("Area of Rectangle is: " + area);
}
public static void main(String[] args) {
double x, y, area;
x = getLength();
y = getWidth();
area = getArea(x, y);
displayData(area);
}
}
Activity 2:
Write a
function called displayShape that
accepts size and shapetype as parameters and displays a square or a triangle of
the required size.
So e.g. if
the function is called with the following parameters:
displayShape(‘S’, 5); a square of
size 5 is displayed as given
1 |
2 |
3 |
4 |
5 |
2 |
4 |
6 |
8 |
10 |
3 |
6 |
9 |
12 |
15 |
4 |
8 |
12 |
16 |
20 |
5 |
10 |
15 |
20 |
25 |
displayShape(‘T’, 6); a triangle
of size 6 is displayed as given
1
1
1
1
2 1
1
3 3 1
1
4 6 4 1
1
5 10
10 5 1
Solution:
import java.util.Scanner;
public class Main {
static void printSquare(int size) {
int num, res;
for (int i = 1; i <= size; i++) {
num = i;
for (int j = 1; j <= size; j++) {
res = num * j;
System.out.print(res + "\t");
}
System.out.print("\n");
}
System.out.print("\n");
}
static void printTriangle(int size) {
int coef = 1;
for (int i = 0; i < size; i++) {
for (int space = 1; space <= size - i; space++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
coef = 1;
}
else {
coef = coef * (i - j + 1) / j;
}
System.out.print(coef + " ");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
char choice2 = 'y';
do {
int size;
char choice;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Size of Shape: ");
size = scanner.nextInt();
System.out.println("Enter s for Square-shape or t for triangle-shape: ");
choice = scanner.next().charAt(0);
if (choice == 's') {
printSquare(size);
}
if (choice == 't') {
printTriangle(size);
}
System.out.println("If you want to Repeat then press y. ");
choice2 = scanner.next().charAt(0);
} while (choice2 == 'y');
scanner.close();
}
}
Activity 3:
Write a program that converts a binary number into decimal number.
Solution:
import java.util.Scanner;
public class Main {
static int decimalConverter(int num) {
int dec = 0, i = 0, rem;
boolean possible = true;
while (num != 0) {
rem = num % 10;
num /= 10;
if (rem == 0 || rem == 1) {
dec += rem * (int)Math.pow(2, i);
++i;
}
else {
System.out.println("Conversion not possible: it is not a binary number.");
possible = false;
break;
}
}
if (possible) {
return dec;
}
return 0; // Need to return a value in all paths
}
public static void main(String[] args) {
int n;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a Binary Number: ");
n = scanner.nextInt();
System.out.println("Decimal Number: " + decimalConverter(n));
scanner.close();
}
}