[go: up one dir, main page]

0% found this document useful (0 votes)
14 views5 pages

public class Exercise14

The document contains Java code examples demonstrating various programming concepts, including displaying an American flag pattern, defining an Employee class with attributes and methods, and performing arithmetic calculations. It includes sample outputs for employee details and arithmetic expressions. Additionally, it showcases list manipulation and character display in a specific pattern.

Uploaded by

Kassa Mamuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

public class Exercise14

The document contains Java code examples demonstrating various programming concepts, including displaying an American flag pattern, defining an Employee class with attributes and methods, and performing arithmetic calculations. It includes sample outputs for employee details and arithmetic expressions. Additionally, it showcases list manipulation and character display in a specific pattern.

Uploaded by

Kassa Mamuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Display American flag


public class Exercise14 {

public static void main(String[] args) {


// Print a pattern of asterisks and equal signs to create a design
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");

// Print a row of equal signs to complete the design


System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
}
}
// Employee.java
// Import the LocalDate class from the java.time package
import java.time.LocalDate;

// Import the Period class from the java.time package


import java.time.Period;

// Define the Employee class


public class Employee {

// Declare a private variable to store the name of the employee


private String name;

// Declare a private variable to store the salary of the employee


private double salary;

// Declare a private variable to store the hire date of the employee


private LocalDate hireDate;

// Constructor for the Employee class


public Employee(String name, double salary, LocalDate hireDate) {
// Initialize the name of the employee
this.name = name;

// Initialize the salary of the employee


this.salary = salary;

// Initialize the hire date of the employee


this.hireDate = hireDate;
}

// Method to get the name of the employee


public String getName() {
// Return the name of the employee
return name;
}

// Method to set the name of the employee


public void setName(String name) {
// Update the name variable to the new value
this.name = name;
}

// Method to get the salary of the employee


public double getSalary() {
// Return the salary of the employee
return salary;
}

// Method to set the salary of the employee


public void setSalary(double salary) {
// Update the salary variable to the new value
this.salary = salary;
}

// Method to get the hire date of the employee


public LocalDate getHireDate() {
// Return the hire date of the employee
return hireDate;
}

// Method to set the hire date of the employee


public void setHireDate(LocalDate hireDate) {
// Update the hire date variable to the new value
this.hireDate = hireDate;
}

// Method to calculate the years of service of the employee


public int getYearsOfService() {
// Calculate the period between the hire date and the current date, and
return the number of years
return Period.between(hireDate, LocalDate.now()).getYears();
}

// Method to print the details of the employee


public void printEmployeeDetails() {
// Print the name of the employee
System.out.println("\nName: " + name);

// Print the salary of the employee


System.out.println("Salary: " + salary);

// Print the hire date of the employee


System.out.println("Hire Date: " + hireDate);
}
}
In the above Employee class, there are three private attributes: name, salary, and hireDate, a
constructor that initializes these attributes with the values passed as arguments, and getter and
setter methods to access and modify these attributes.

There is a method “getYearsOfService()” to calculate years of service between the hire date and
the current date, and returns the number of years as an integer value.

// Main.java
// Import the LocalDate class from the java.time package
import java.time.LocalDate;

// Define the Main class


public class Main {

// Main method, the entry point of the Java application


public static void main(String[] args) {

// Create a new Employee object named employee1 with name "Roberta


Petrus", salary 50000, and hire date "2021-04-01"
Employee employee1 = new Employee("Roberta Petrus", 50000,
LocalDate.parse("2021-04-01"));

// Create a new Employee object named employee2 with name "Loyd Blair",
salary 70000, and hire date "2015-04-01"
Employee employee2 = new Employee("Loyd Blair", 70000,
LocalDate.parse("2015-04-01"));

// Create a new Employee object named employee3 with name "Magdalena


Artemon", salary 50000, and hire date "2011-01-15"
Employee employee3 = new Employee("Magdalena Artemon", 50000,
LocalDate.parse("2011-01-15"));

// Print the details of employee1


employee1.printEmployeeDetails();

// Print the years of service of employee1


System.out.println("Years of Service: " + employee1.getYearsOfService());

// Print the details of employee2


employee2.printEmployeeDetails();

// Print the years of service of employee2


System.out.println("Years of Service: " + employee2.getYearsOfService());

// Print the details of employee3


employee3.printEmployeeDetails();

// Print the years of service of employee3


System.out.println("Years of Service: " + employee3.getYearsOfService());
}
}
Sample Output:

Name: Roberta Petrus


Salary: 50000.0
HireDate: 2021-04-01
Years of Service: 2

Name: Loyd Blair


Salary: 70000.0
HireDate: 2015-04-01
Years of Service: 8

Name: Magdalena Artemon


Salary: 50000.0
HireDate: 2011-01-15
Years of Service: 12

Java Code:

import java.util.*;
public class Exercise2 {
public static void main(String[] args) {
// Creae a list and add some colors to the list
List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
// Print the list
for (String element : list_Strings) {
System.out.println(element);
}
}
}

Sample Output:

Red
Green
Orange
White
Black
public class Exercise4 {
public static void main(String[] args) {
// Calculate and print the result of the expression: -5 + 8 * 6
System.out.println(-5 + 8 * 6);

// Calculate and print the result of the expression: (55 + 9) % 9


System.out.println((55 + 9) % 9);

// Calculate and print the result of the expression: 20 + -3 * 5 / 8


System.out.println(20 + -3 * 5 / 8);
// Calculate and print the result of the expression: 5 + 15 / 3 * 2 -
8 % 3
System.out.println(5 + 15 / 3 * 2 - 8 % 3);
}
}

Explanation:

The above Java code calculates and prints the results of four different arithmetic expressions:

'-5 + 8 * 6' evaluates to '43'.

'(55 + 9) % 9' evaluates to '1'.

'20 + -3 * 5 / 8' evaluates to '19'.

'5 + 15 / 3 * 2 - 8 % 3' evaluates to '13'.

public class Exercise3 {


public static void main(String[] args) {
// Calculate the result of the division 50/3
int result = 50 / 3;

// Print the result of the division


System.out.println(result);
}
}
public class Exercise8 {

public static void main(String[] args) {


// Display the characters to form the text "Java" in a specific
pattern
System.out.println(" J a v v a ");
System.out.println(" J a a v v a a");
System.out.println("J J aaaaa V V aaaaa");
System.out.println(" JJ a a V a a");
}
}
Sample Output:
J a v v a
J a a v va a
J J aaaaa V V aaaaa
JJ a a V a a

You might also like