Java record unit-1
Java record unit-1
Java Programming23CSR306
Ex no: 1 Develop programs using flow control statements and arrays to manage
Date: execution flow and data organization effectively
Aim:
To develop programs using flow control statements and arrays to manage execution flow and data
organization effectively.
Question 1:
Write a program that displays a menu with options 1. Add 2. Sub Based on the options chosen, read
2 numbers and perform the relevant operation. After performing the operation, the program should
ask the user if he wants to continue. If the user presses y or Y, then the program should continue
displaying the menu else the program should terminate. [ Note: Use Scanner class, you can take help
from the trainer regarding the same]
Code:
import java.util.Scanner;
class ArithOperation
{
public static void main(String[] args)
{
int num1,num2,choice;
char repeat;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number1:");
num1=sc.nextInt();
System.out.println("Enter number2:");
num2=sc.nextInt();
do
{
System.out.println("Arithmetic operations");
System.out.println("1.Add, 2.Sub, 3.Mul, 4.Div");
System.out.println("Enter your choice");
choice=sc.nextInt();
switch(choice)
Devadharshini . P 717823L310
Page 2 of 17
Java Programming23CSR306
{
case 1:
System.out.println(num1+num2);
break;
case 2:
System.out.println(num1-num2);
break;
default:
System.out.println("Enter a valid choice");
break;
}
System.out.println("Do you want to continue: (y/n)");
repeat = sc.next().charAt(0);
Devadharshini . P 717823L310
Page 3 of 17
Java Programming23CSR306
Output
Devadharshini . P 717823L310
Page 4 of 17
Java Programming23CSR306
Question 2:
Write a program to print the sum of the elements of the array with the given below condition. If
the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the
calculation of sum. Eg1) Array Elements - 10,3,6,1,2,7,9 O/P: 22 [i.e 10+3+9] Eg2) Array
Elements - 7,1,2,3,6 O/P:19 Eg3) Array Elements - 1,6,4,7,9 O/P:10
Aim:
To write a program to print the sum of the elements of the array according to the given
condition.
Code:
package unit1;
import java.util.Scanner;
Devadharshini . P 717823L310
Page 5 of 17
Java Programming23CSR306
Devadharshini . P 717823L310
Page 6 of 17
Java Programming23CSR306
Output:
Devadharshini . P 717823L310
Page 7 of 17
Java Programming23CSR306
Question 3:
Create a new class called “Calculator” which contains the following:
• A static method called powerInt(int num1,int num2) that accepts two integers and returns
num1 to the power of num2 (num1 power num2).
• A static method called powerDouble(double num1,int num2) that accepts one double and one
integer and returns num1 to the power of num2 (num1 power num2).
• Call your method from another class without instantiating the class (i.e. call it like
Calculator.powerInt(12,10) since your methods are defined to be static) Hint: Use
Math.pow(double,double) to calculate the power.
Aim:
To create a new class called “Calculator” which contains the given methods.
Code:
package BasicJava;
public class Calculator {
public static int powerInt(int num1, int num2) {
return (int) Math.pow(num1, num2);
}
public static double powerDouble(double num1, int num2) {
return Math.pow(num1, num2);
}
}
package BasicJava;
Devadharshini . P 717823L310
Page 8 of 17
Java Programming23CSR306
Output:
Devadharshini . P 717823L310
Page 9 of 17
Java Programming23CSR306
Question 4:
Consider the following UML class diagram:
• Write a Java class for the Circle class based on the UML class
diagram.
• Write a main class TestCircle that creates two objects for the
Circle class and invoke all the methods.
Aim:
To convert the UML diagram to code.
Code:
package BasicJava;
public class Circle {
public Circle() {
}
Devadharshini . P 717823L310
Page 10 of 17
Java Programming23CSR306
Devadharshini . P 717823L310
Page 11 of 17
Java Programming23CSR306
circle1.setRadius(2.5);
circle1.setColor("blue");
System.out.println("\nModified Circle 1:");
System.out.println("Radius: " + circle1.getRadius());
System.out.println("Color: " + circle1.getColor());
System.out.println("Area: " + circle1.getArea());
System.out.println(circle1.toString());
Devadharshini . P 717823L310
Page 12 of 17
Java Programming23CSR306
Output:
Devadharshini . P 717823L310
Page 13 of 17
Java Programming23CSR306
Question 5:
Consider the following UML class diagram:
• Write a Java class for the Employee class based on the UML class
diagram.
• Write a main class TestEmployee that creates two objects for the
Employee class and invoke all the methods.
Aim:
To convert the UML diagram to code.
Code:
package BasicJava;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
Devadharshini . P 717823L310
Page 14 of 17
Java Programming23CSR306
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
Devadharshini . P 717823L310
Page 15 of 17
Java Programming23CSR306
Devadharshini . P 717823L310
Page 16 of 17
Java Programming23CSR306
Output:
Devadharshini . P 717823L310
Page 17 of 17
Java Programming23CSR306
Result:
Thus, the Java programs using flow control statements and arrays to manage execution flow and data
organization has been successfully developed and the output was verified.
Devadharshini . P 717823L310