Page 1 of 17
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);
}while(repeat == 'y' || repeat == 'Y');
sc.close();
}
}
                                                        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;
        public class SumOfArray {
          public static void main(String[] args) {
             Scanner sc = new Scanner(System.in);
             int Size, Sum = 0, SixPos = -1, SevenPos = -1;
             System.out.println("Enter size of the array:");
             Size = sc.nextInt();
             int[] num = new int[Size];
             System.out.println("Enter the elements of the array:");
             for (int i = 0; i < Size; i++) {
                 num[i] = sc.nextInt();
                 if (num[i] == 6 && SixPos == -1) {
                     SixPos = i;
                 }
                 if (num[i] == 7 && SevenPos == -1) {
                     SevenPos = i;
                 }
             }
                                                                         Devadharshini . P 717823L310
                                                                                       Page 5 of 17
                                                                        Java Programming23CSR306
        for (int i = 0; i < Size; i++) {
            if (SixPos != -1 && SevenPos != -1 && i >= SixPos && i <= SevenPos) {
                continue;
            }
            Sum += num[i];
        }
        System.out.println("Sum: " + Sum);
    }
}
                                                                        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;
public class Main {
            public static void main(String[] args) {
                int resultInt = Calculator.powerInt(12, 10);
                System.out.println("12 to the power of 10 is: " + resultInt);
                double resultDouble = Calculator.powerDouble(2.5, 3);
                System.out.println("2.5 to the power of 3 is: " + resultDouble);
            }
}
                                                                                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 {
           private double radius = 1.0;
           private String color = "red";
           public Circle() {
           }
           public Circle(double radius) {
               this.radius = radius;
           }
                                                                   Devadharshini . P 717823L310
                                                                                           Page 10 of 17
                                                                             Java Programming23CSR306
          public Circle(double radius, String color) {
              this.radius = radius;
              this.color = color;
          }
          public double getRadius() {
              return radius;
          }
          public String getColor() {
              return color;
          }
          public void setRadius(double radius) {
              this.radius = radius;
          }
          public void setColor(String color) {
              this.color = color;
          }
          public String toString() {
              return "Circle[radius=" + radius + ", color=" + color + "]";
          }
          public double getArea() {
              return 3.14 * radius * radius;
          }
      }
package BasicJava;
                                                                             Devadharshini . P 717823L310
                                                                                     Page 11 of 17
                                                                       Java Programming23CSR306
public class TestCircle {
           public static void main(String[] args) {
               Circle circle1 = new Circle();
               System.out.println("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());
               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());
               Circle circle2 = new Circle(3.0, "green");
               System.out.println("\nCircle 2:");
               System.out.println("Radius: " + circle2.getRadius());
               System.out.println("Color: " + circle2.getColor());
               System.out.println("Area: " + circle2.getArea());
               System.out.println(circle2.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;
           public Employee(int id, String firstName, String lastName, int salary) {
                                                                           Devadharshini . P 717823L310
                                                       Page 14 of 17
                                         Java Programming23CSR306
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.salary = salary;
}
public int getID() {
    return id;
}
public String getFirstName() {
    return firstName;
}
public String getLastName() {
    return lastName;
}
public String getName() {
    return firstName + " " + lastName;
}
public int getSalary() {
    return salary;
}
public void setSalary(int salary) {
    this.salary = salary;
}
public int getAnnualSalary() {
                                         Devadharshini . P 717823L310
                                                                                     Page 15 of 17
                                                                        Java Programming23CSR306
             return salary * 12;
         }
         public int raiseSalary(int percent) {
             salary += salary * percent / 100;
             return salary;
         }
         public String toString() {
              return "Employee[id=" + id + ",name=" + firstName + " " + lastName + ",salary="
+ salary + "]";
         }
                                                                       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