Java Assignment
Java Assignment
Programming Paradigms
A programming paradigm is a fundamental style or approach to programming that provides a
way to structure and organize code. Paradigms influence the design and implementation of
programs by providing specific principles and methodologies. Understanding different
paradigms is crucial for selecting the appropriate approach for a given problem and for
becoming a versatile and effective programmer.
Imperative Programming
Declarative Programming
Functional Programming
Procedural Programming
Logic Programming
Event-Driven Programming
1. Imperative Programming
Imperative programming is a paradigm that expresses computation in terms of statements that
change a program's state. It focuses on describing how a program operates, using statements
that explicitly change the program's state.
Characteristics:
Copy
// Example in C
int sum(int n) {
int total = 0;
total += i;
return total;
2. Declarative Programming
Declarative programming is a paradigm that expresses the logic of computation without
describing its control flow. It focuses on what the program should accomplish rather than how
to accomplish it.
Characteristics:
sql
Copy
-- Example in SQL
Characteristics:
Polymorphism: Ability to treat objects of different classes through the same interface.
Abstraction: Hiding complex implementation details and showing only the necessary features.
java
Copy
// Example in Java
class Animal {
String name;
void makeSound() {
System.out.println("Some sound...");
void makeSound() {
System.out.println("Bark");
}
4. Functional Programming
Functional programming is a paradigm where programs are constructed by applying and
composing functions. It emphasizes the use of pure functions, immutability, and avoiding side-
effects.
Example Languages: Haskell, Scala, Erlang, Lisp, Python (when used functionally)
Characteristics:
haskell
Copy
-- Example in Haskell
sumList [] = 0
5. Procedural Programming
Procedural programming is a paradigm derived from structured programming, based on the
concept of procedure calls. Procedures, also known as routines, subroutines, or functions,
contain a series of computational steps to be carried out.
Copy
// Example in C
void greet() {
printf("Hello, World!");
6. Logic Programming
Logic programming is a paradigm based on formal logic. Programs are expressed in terms of
relations, and execution is the process of proving queries using these relations.
Characteristics:
7 Event-driven programming
Event-driven programming is a paradigm in which the flow of the program is determined by
events such as user actions (mouse clicks, key presses), sensor outputs, or message passing
from other programs.
Example Languages: JavaScript, C#, Visual Basic
Characteristics:
javascript
Copy
// Example in JavaScript
document.getElementById('myButton').addEventListener('click', function() {
});
Summary
Declarative Programming: Focuses on what the outcome should be, without specifying how to
achieve it.
Object-Oriented Programming (OOP): Organizes code around objects and data encapsulation.
Event-Driven Programming: Driven by events and event handlers, commonly used in GUIs and
real-time systems.
Each paradigm can be more effective depending on the task at hand. By understanding these
paradigms, developers can choose the most suitable approach, write better code, and solve
problems more efficiently.
Program of calculator
import java.util.Scanner;
scanner.close();
}
public static int sum(int a, int b) {
return a + b;
return a * b;
if (b == 0) {
return 0;
return a / b;
return a - b;
}
Program of to select head of department
import java.util.Scanner;
String message;
switch (department.toLowerCase()) {
case 1:"mulgeta":
system.out.println();
break;
case 2: "gebremariam":
break;
case 3:"tedros":
default:
break;
System.out.println(message);
scanner.close();
import java.util.Scanner;
matrix[i][j] = scanner.nextInt();
printMatrix(matrix);
printMatrix(resultMatrix);
scanner.close();
}
}
return result;
System.out.println();
}
Multiplication of two matrices (3X3)
import java.util.Scanner;
matrix1[i][j] = scanner.nextInt();
printMatrix(resultMatrix);
scanner.close();
}
return result;
System.out.println();
}
please write a program about geometric for example circle ,
rectangle, triangle, using constructor Java
// Base constructor
this.radius = radius;
@Override
}
@Override
this.width = width;
this.height = height;
@Override
@Override
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
@Override
@Override
int sum=0;
x=D.nextInt();
y=C.nextInt();
for(x=0;x<=y;x++)
sum=sum+x;
System.out.println(sum);
Full Grade
import java.util.Scanner;
String grade ;
while(true){
double mark=A.nextDouble();
if(mark>=100){
grade="check";
else if (mark>=90){
grade="A+";
else if (mark>=85){
grade="A";
else if (mark>=80){
grade="A-";
else if (mark>=75){
grade="B+";
else if(mark>=70){
grade="B";
else if (mark>=65){
grade="B-";
else if (mark>=60){
grade="C+";
}
else if (mark>=50){
grade="C";
else if (mark>=50){
grade="C-";
else if (mark>=40){
grade="D";
else if (mark<40){
grade="F";
else if (mark<0){
grade="invalid";
else if (mark>100){
grade="F";
System.out.println(grade);
}
}
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
private final String[] COUNTRIES = {"Select Country", "USA", "Canada", "UK", "Australia"};
private final String[] REGIONS = {"Select Region", "East", "West", "North", "South"};
public lg() {
frame.setSize(21000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
panel.setLayout(null);
panel.add(usernameLabel);
panel.add(usernameField);
usernameField.setCursor(new Cursor(Cursor.HAND_CURSOR));
usernameField.addMouseListener(new MouseAdapter(){
usernameField.setBackground(Color.RED);
usernameField.setForeground(Color.BLACK);
usernameField.setBackground(UIManager.getColor("control"));
usernameField.setForeground(Color.red);
}});
panel.add(lastLabel);
panel.add(lastField);
lastField.setCursor(new Cursor(Cursor.HAND_CURSOR));
lastField.addMouseListener(new MouseAdapter(){
lastField.setBackground(Color.RED);
lastField.setForeground(Color.BLACK);
lastField.setBackground(UIManager.getColor("control"));
lastField.setForeground(Color.BLACK);
}});
panel.add(IdLabel);
panel.add(IdField);
IdField.setCursor(new Cursor(Cursor.HAND_CURSOR));
IdField.addMouseListener(new MouseAdapter(){
IdField.setForeground(Color.BLACK);
IdField.setBackground(UIManager.getColor("control"));
IdField.setForeground(Color.red);
}});
panel.add(ageLabel);
panel.add(ageField);
ageField.setCursor(new Cursor(Cursor.HAND_CURSOR));
ageField.addMouseListener(new MouseAdapter(){
ageField.setBackground(Color.RED);
ageField.setForeground(Color.BLACK);
}
public void mouseExited(MouseEvent evt) {
ageField.setBackground(UIManager.getColor("control"));
ageField.setForeground(Color.red);
}});
panel.add(passwordLabel);
panel.add(passwordField);
passwordField.setCursor(new Cursor(Cursor.HAND_CURSOR));
passwordField.addMouseListener(new MouseAdapter(){
passwordField.setBackground(Color.RED);
passwordField.setForeground(Color.BLACK);
}
public void mouseExited(MouseEvent evt) {
passwordField.setBackground(UIManager.getColor("control"));
passwordField.setForeground(Color.red);
}});
panel.add(sexLabel);
panel.add(sexComboBox);
sexComboBox.setCursor(new Cursor(Cursor.HAND_CURSOR));
sexComboBox.addMouseListener(new MouseAdapter(){
sexComboBox.setBackground(Color.RED);
sexComboBox.setForeground(Color.BLACK);
sexComboBox.setBackground(UIManager.getColor("control"));
sexComboBox.setForeground(Color.red);
}});
departmentLabel = new JLabel("department:");
panel.add(departmentLabel);
panel.add(departmentComboBox);
departmentComboBox.setCursor(new Cursor(Cursor.HAND_CURSOR));
departmentComboBox.addMouseListener(new MouseAdapter(){
departmentComboBox.setBackground(Color.RED);
departmentComboBox.setForeground(Color.BLACK);
departmentComboBox.setBackground(UIManager.getColor("control"));
departmentComboBox.setForeground(Color.red);
}});
panel.add(phoneLabel);
panel.add(phoneField);
phoneField.setCursor(new Cursor(Cursor.HAND_CURSOR));
phoneField.addMouseListener(new MouseAdapter(){
phoneField.setBackground(Color.RED);
phoneField.setForeground(Color.BLACK);
phoneField.setBackground(UIManager.getColor("control"));
phoneField.setForeground(Color.red);
}});
panel.add(emailLabel);
emailField.setCursor(new Cursor(Cursor.HAND_CURSOR));
emailField.addMouseListener(new MouseAdapter(){
emailField.setBackground(Color.RED);
emailField.setForeground(Color.BLACK);
emailField.setBackground(UIManager.getColor("control"));
emailField.setForeground(Color.red);
}});
panel.add(addressLabel);
panel.add(addressField);
addressField.setCursor(new Cursor(Cursor.HAND_CURSOR));
addressField.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent evt) {
addressField.setBackground(Color.RED);
addressField.setForeground(Color.BLACK);
addressField.setBackground(UIManager.getColor("control"));
addressField.setForeground(Color.red);
}});
panel.add(passportLabel);
panel.add(passportField);
passportField.setCursor(new Cursor(Cursor.HAND_CURSOR));
passportField.addMouseListener(new MouseAdapter(){
passportField.setBackground(Color.RED);
passportField.setForeground(Color.BLACK);
}
public void mouseExited(MouseEvent evt) {
passportField.setBackground(UIManager.getColor("control"));
passportField.setForeground(Color.red);
}});
panel.add(countryLabel);
panel.add(countryComboBox);
countryComboBox.setCursor(new Cursor(Cursor.HAND_CURSOR));
countryComboBox.addMouseListener(new MouseAdapter(){
countryComboBox.setBackground(Color.RED);
countryComboBox.setForeground(Color.BLACK);
countryComboBox.setBackground(UIManager.getColor("control"));
countryComboBox.setForeground(Color.red);
}});
panel.add(regionLabel);
panel.add(regionComboBox);
regionComboBox.setCursor(new Cursor(Cursor.HAND_CURSOR));
regionComboBox.addMouseListener(new MouseAdapter(){
regionComboBox.setBackground(Color.RED);
regionComboBox.setForeground(Color.BLACK);
regionComboBox.setBackground(UIManager.getColor("control"));
regionComboBox.setForeground(Color.red);
}});
panel.add(registerButton);
registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
registerButton.addMouseListener(new MouseAdapter(){
registerButton.setBackground(Color.RED);
registerButton.setForeground(Color.BLACK);
registerButton.setBackground(UIManager.getColor("control"));
registerButton.setForeground(Color.red);
}});
exitButton.addActionListener(this);
panel.add(exitButton);
exitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
exitButton.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent evt) {
exitButton.setBackground(Color.RED);
exitButton.setForeground(Color.BLACK);
exitButton.setBackground(UIManager.getColor("control"));
exitButton.setForeground(Color.red);
}});
fieldArea.setEditable(false);
panel.add(scrollPane);
fieldArea.setCursor(new Cursor(Cursor.HAND_CURSOR));
fieldArea.addMouseListener(new MouseAdapter(){
fieldArea.setBackground(Color.RED);
fieldArea.setForeground(Color.BLACK);
}
public void mouseExited(MouseEvent evt) {
fieldArea.setBackground(UIManager.getColor("control"));
fieldArea.setForeground(Color.darkGray);
}});
panel.add(welLabel);
frame.add(panel);
frame.setVisible(true);
new lg();
lg sys=new lg();
sys.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
System.exit(0);
if (e.getSource() == registerButton) {
String id = IdField.getText();
fieldArea.setText(registrationData);
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationsystem","root" ,
"1234567890");
Statement st=con.createStatement();
String registerButton = e.getActionCommand();
if(registerButton.equals("register")){
String Choice;
do{
String fname1=usernameField.getText();
String fathername1=lastField.getText();
String Age=ageField.getText();
String sex=sexComboBox.getItemAt(sexComboBox.getSelectedIndex());
String
department1=departmentComboBox.getItemAt(departmentComboBox.getSelectedIndex());
String phone=phoneField.getText();
String Addres=addressField.getText();
String passport1=passportField.getText();
String email1=emailField.getText();
String
country1=countryComboBox.getItemAt(countryComboBox.getSelectedIndex());
String regoin=regionComboBox.getItemAt(regionComboBox.getSelectedIndex());
pst.setString(1, String.valueOf(id1));
pst.setString(2, fname1);
pst.setString(3, fathername1);
pst.setString(4, String.valueOf(Age));
pst.setString(5, String.valueOf(sex));
pst.setString(6,department1);
pst.setString(7,phone );
pst.setString(8,Addres);
pst.setString(9,passport1);
pst.setString(10,email1);
pst.setString(11,country1);
pst.setString(12,regoin);
int result=pst.executeUpdate();
if(result>=0){
else{
//JOptionPane.showMessageDialog(null,"failed to register");
}
//JOptionPane.showMessageDialog(null,"do you went to continue/press Yes and press
anther word to stop");
while(Choice.equals("Yes"));
}else{
JOptionPane.showMessageDialog(null,"failed to register");
lg sys=new lg();
sys.setVisible(true);
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
void show() {
}
import java.util.*;
class ConvertCasePrg
String str="";
//input string
str=sc.nextLine();
String lowerCaseString="",upperCaseString="";
lowerCaseString= str.toLowerCase();
double num1 = 0;
double num2 = 0;
double num3 = 0;
num1 = scan.nextDouble();
num2 = scan.nextDouble();
num3 = scan.nextDouble();
avg = sum/3;
// display result
}
Program of to identify odd and even
import java.util.Scanner;
if(num % 2 == 0)
else