0% found this document useful (0 votes)
0 views

LAB05 TASKS oop.

The document outlines various programming tasks related to methods, user-defined examples, and constructors in Java. It includes code snippets for finding even/odd numbers, counting vowels, calculating the area of a rectangle, and displaying student details. Additionally, it features programs for checking if every digit in a number is even and displaying the current date and time.

Uploaded by

fouzianawaz23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

LAB05 TASKS oop.

The document outlines various programming tasks related to methods, user-defined examples, and constructors in Java. It includes code snippets for finding even/odd numbers, counting vowels, calculating the area of a rectangle, and displaying student details. Additionally, it features programs for checking if every digit in a number is even and displaying the current date and time.

Uploaded by

fouzianawaz23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT

LAB 05 TASKS
METHODS
TYPES OF METHODS
PRE-DEFINE EXAMPLE
public class PreDefineexample {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.print("The maximum number is: " + Math.max(9,7));

}
OUTPUT

USER DEFINE EXAMPLE


public class UserDefine {

public static void main(String[] args) {


// TODO Auto-generated method stub
public static void findEvenOdd(int num); {
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}

TASKS
PROGRAM
package tasks;
import java.util.Scanner;
public class Exercise {

public static void main(String[] args) {


// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);


System.out.println("Input a string: ");
String input = scanner.next();
System.out.println("The middle character in the string: " +
getMiddleCharacter(input));
}

public static String getMiddleCharacter(String str) {


int length = str.length();
int middle = length / 2;

if (length % 2 != 0) {
return String.valueOf(str.charAt(middle));
}

else {
return str.substring(middle - 1, middle + 1);
}
}
}
OUTPUT

PROGRAM
import java.util.Scanner;
public class CountVowels {

public static void main(String[] args) {


// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);


System.out.println("Enter a string: ");
String input = scanner.nextLine();
System.out.println("Number of vowels: " + countVowels(input));
}
public static int countVowels(String str) {
int count = 0;
str = str.toLowerCase();

for (int i = 0; i < str.length(); i++) {


char ch = str.charAt(i);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch


== 'u') {
count++;
}
}

return count;
}

OUTPUT

public class Rectangle {


int length;
int width;

Rectangle() {
length = 0;
width = 0;
}

Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
void display() {
System.out.println("Length: " + length + ", Width: " + width);
System.out.println("Area: " + area());
}
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Rectangle rect1 = new Rectangle();
System.out.println("Rectangle 1 (Default Constructor):");
rect1.display();

Rectangle rect2 = new Rectangle(5, 3);


System.out.println("Rectangle 2 (Parameterized Constructor):");
rect2.display();
}
}

CONSTRUCTOR TASK
public class Student {
private String name;
private int age;
private char grade;

// Default constructor
public Student() {
this.name = "Unknown";
this.age = 0;
this.grade = 'N';
}

// Parameterized constructor
public Student(String name, int age, char grade) {
this.name = name;
this.age = age;
this.grade = grade;
}

// Method to display student details


public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
}

// Getters and setters


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

public char getGrade() {


return grade;
}

public void setGrade(char grade) {


this.grade = grade;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Student defaultStudent = new Student();
System.out.println("Default Student Details:");
defaultStudent.displayDetails();

// Create object using parameterized constructor


Student parameterizedStudent = new Student(" FOZIA RAJPUT", 20, 'A');
System.out.println("\nParameterized Student Details:");
parameterizedStudent.displayDetails();
}
}

LAB TASKS
Program 01
public class Main {

public static void main(String[] args) {


// TODO Auto-generated method stub

System.out.println(isEveryDigitEven(8642)); // true
System.out.println(isEveryDigitEven(123)); // false
System.out.println(isEveryDigitEven(200)); // true
}

public static boolean isEveryDigitEven(int num) {


while (num != 0) {
int digit = num % 10;
if (digit % 2 != 0) { // Check if digit is odd
return false;
}
num /= 10;
}
return true;
}
}
OUTPUT
Program 02
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTime {

public static void main(String[] args) {


// TODO Auto-generated method stub
displayCurrentDateTime();
}

public static void displayCurrentDateTime() {


LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss");

String formattedDateTime = currentDateTime.format(formatter);


System.out.println("Current Date and Time: " + formattedDateTime);
}
}

OUTPUT

You might also like