0% found this document useful (0 votes)
7 views12 pages

Week - 3

The document contains several Java programming examples demonstrating key concepts such as creating classes with inner classes, method overloading, constructor overloading, character counting, student marks management, and email validation. Each section includes a specific aim, code implementation, and output results. The examples illustrate practical applications of Java programming techniques and basic data handling.

Uploaded by

Balu Kongari
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)
7 views12 pages

Week - 3

The document contains several Java programming examples demonstrating key concepts such as creating classes with inner classes, method overloading, constructor overloading, character counting, student marks management, and email validation. Each section includes a specific aim, code implementation, and output results. The examples illustrate practical applications of Java programming techniques and basic data handling.

Uploaded by

Balu Kongari
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

1Aim:

Create a class Appointment to store patient name, doctor name, and date. Use inner class
Patient with fields for age and contact number. Demonstrate how inner class works by
creating an appointment object and calling inner class methods.

Programme:

public class Appointment {

private String patientName;

private String doctorName;

private String date;

// Constructor for Appointment

public Appointment(String patientName, String doctorName, String date) {

[Link] = patientName;

[Link] = doctorName;

[Link] = date;

// Method to display appointment details

public void showAppointmentDetails() {

[Link]("Appointment Details:");

[Link]("Patient Name: " + patientName);

[Link]("Doctor Name: " + doctorName);

[Link]("Date: " + date);

// Inner class Patient

public class Patient {

private int age;

private String contactNumber;


// Constructor for Patient

public Patient(int age, String contactNumber) {

[Link] = age;

[Link] = contactNumber;

// Method to display patient details

public void showPatientDetails() {

[Link]("Patient Details:");

[Link]("Age: " + age);

[Link]("Contact Number: " + contactNumber);

// Main method to test inner class

public static void main(String[] args) {

// Create an Appointment object

Appointment app = new Appointment("John Doe", "Dr. Smith", "2025-12-20");

[Link]();

// Create an inner class Patient object

[Link] patient = [Link] Patient(30, "9876543210");

[Link]();

}
Output:

Appointment Details:

Patient Name: John Doe

Doctor Name: Dr. Smith

Date: 2025-12-20

Patient Details:

Age: 30

Contact Number: 9876543210

[Link]:

Programme for Method Overloading in Java:

Programme:

class Calculator {

// Method 1 – adds two integers

int add(int a, int b) {

return a + b;

// Method 2 – adds three integers

int add(int a, int b, int c) {

return a + b + c;

// Method 3 – adds two double numbers

double add(double a, double b) {

return a + b;

public class Main {

public static void main(String[] args) {

Calculator calc = new Calculator();


[Link]("Add two int: " + [Link](10, 20)); // 30

// [Link]("Add three int: " + [Link](10, 20, 30)); // 60

// [Link]("Add two double: " + [Link](5.5, 2.5)); // 8.0

Output

Add two int: 30

Add three int: 60

Add two double: 8.0

[Link]:

Implement a class Product with id, name, and price. Use constructor overloading to
initialize different combinations of attributes. Use arrays to store 3 product objects and
display their details.

Programme:

public class Product {

private int id;

private String name;

private double price;

// Constructor 1: Initialize all attributes

public Product(int id, String name, double price) {

[Link] = id;

[Link] = name;

[Link] = price;

}
// Constructor 2: Initialize id and name only

public Product(int id, String name) {

[Link] = id;

[Link] = name;

[Link] = 0.0; // default price

// Constructor 3: Initialize name and price only

public Product(String name, double price) {

[Link] = 0; // default id

[Link] = name;

[Link] = price;

// Method to display product details

public void display() {

[Link]("Product ID: " + id);

[Link]("Product Name: " + name);

[Link]("Product Price: $" + price);

[Link]("----------------------");

// Main method

public static void main(String[] args) {

// Create an array to store 3 products

Product[] products = new Product[3];

// Initialize products using different constructors


products[0] = new Product(101, "Laptop", 75000.0); // all attributes

products[1] = new Product(102, "Mouse"); // id and name only

products[2] = new Product("Keyboard", 1200.0); // name and price only

// Display details of all products

for (Product p : products) {

[Link]();

Output:

markdown

Copy code

Product ID: 101

Product Name: Laptop

Product Price: $75000.0

----------------------

Product ID: 102

Product Name: Mouse

Product Price: $0.0

----------------------

Product ID: 0

Product Name: Keyboard

Product Price: $1200.0

----------------------

[Link]:

Write a Java program to count the number of vowels, consonants, digits, and special
characters in a given string.
Programme:

import [Link];

public class CharacterCounter {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Input string from user

[Link]("Enter a string: ");

String input = [Link]();

// Initialize counters

int vowels = 0, consonants = 0, digits = 0, specialChars = 0;

// Convert string to lowercase for easier comparison

String lowerCaseInput = [Link]();

// Loop through each character

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if (ch >= 'a' && ch <= 'z') {

// Check if vowel or consonant

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

vowels++;

} else {

consonants++;

}
} else if (ch >= '0' && ch <= '9') {

digits++;

} else {

// If not letter or digit, consider special character

specialChars++;

// Display results

[Link]("Vowels: " + vowels);

[Link]("Consonants: " + consonants);

[Link]("Digits: " + digits);

[Link]("Special Characters: " + specialChars);

[Link]();

OutPut:

Enter a string: Hello World! 123

Vowels: 3

Consonants: 7

Digits: 3

Special Characters: 3

Explanation:

'Hello World! 123' contains:

Vowels: e, o, o → 3

Consonants: h, l, l, w, r, l, d → 7
Digits: 1, 2, 3 → 3

Special characters: space + ! → 3

[Link]:

Programme:

You are tasked with building a system to store the marks of 5 students in a class. After
storing their marks in an array, you need to:

a. Calculate the average marks.

B Find the highest and lowest marks.

[Link] the marks of all the students.

import [Link];

public class StudentMarks {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int[] marks = new int[5]; // Array to store marks of 5 students

int sum = 0;

int highest, lowest;

// Input marks from the user

[Link]("Enter marks for 5 students:");

for (int i = 0; i < [Link]; i++) {

[Link]("Student " + (i + 1) + ": ");

marks[i] = [Link]();

sum += marks[i]; // Sum for average calculation

}
// Calculate average

double average = sum / (double) [Link];

// Find highest and lowest marks

highest = marks[0];

lowest = marks[0];

for (int i = 1; i < [Link]; i++) {

if (marks[i] > highest) {

highest = marks[i];

if (marks[i] < lowest) {

lowest = marks[i];

// Display results

[Link]("\nMarks of all students:");

for (int i = 0; i < [Link]; i++) {

[Link]("Student " + (i + 1) + ": " + marks[i]);

[Link]("\nAverage Marks: " + average);

[Link]("Highest Marks: " + highest);

[Link]("Lowest Marks: " + lowest);

[Link]();

}
}

Output:

Enter marks for 5 students:

Student 1: 85

Student 2: 78

Student 3: 92

Student 4: 60

Student 5: 88

Marks of all students:

Student 1: 85

Student 2: 78

Student 3: 92

Student 4: 60

Student 5: 88

Average Marks: 80.6

Highest Marks: 92

Lowest Marks: 60

[Link]:

In many applications, users need to enter their email addresses during registration or
login. To ensure that the email is valid, we need to check if it follows the standard email
format (i.e., contains @, a domain name, and a proper extension like .com, .org, etc.).

We can use Strings in Java to validate the email input. Here's a simple Java program that
checks whether a given email address is valid or not.

Programme:

import [Link];
public class EmailValidator {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Input email from user

[Link]("Enter your email address: ");

String email = [Link]();

// Simple regex for email validation

String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";

// Check if email matches the pattern

if ([Link](emailPattern)) {

[Link]("Valid email address.");

} else {

[Link]("Invalid email address.");

[Link]();

Output:

Enter your email address: [Link]@[Link]

Valid email address.

Enter your email address: [Link]#[Link]

Invalid email address.

You might also like