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

Compile and Execute Java Programs

Uploaded by

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

Compile and Execute Java Programs

Uploaded by

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

1. Lincoln2.

java

public class Lincoln2{public static void main(String[]args){


System.out.println("My name is:");
System.out.println("Ignatius Timothy Bonario");}}

2. Lincoln3.java

//********************************************************************

// Lincoln3.java Java Foundations

//

// Demonstrates another valid program that is poorly formatted.

//********************************************************************

public class

Lincoln3

{
public

static

void

main

String

[]

args )

System.out.println (

"My name is:" )

; System.out.println

"Ignatius Timothy Bonario"

}
3. Abstract about yourself

// Abstract class representing a generic person


abstract class Person {
private String name;
private int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Abstract method to be implemented by subclasses


public abstract void introduce();

// Getter methods
public String getName() {
return name;
}

public int getAge() {


return age;
}
}

// Concrete class representing a specific type of person (e.g., Student)


class Student extends Person {
private String studentId;

// Constructor
public Student(String name, int age, String studentId) {
super(name, age);
this.studentId = studentId;
}

// Implementation of the abstract method


@Override
public void introduce() {
System.out.println("Hi, I'm a student named " + getName() + " with ID " +
studentId + ".");
}
}

// Concrete class representing another type of person (e.g., Teacher)


class Teacher extends Person {
private String employeeId;

// Constructor
public Teacher(String name, int age, String employeeId) {
super(name, age);
this.employeeId = employeeId;
}

// Implementation of the abstract method


@Override
public void introduce() {
System.out.println("Hello, I'm a teacher named " + getName() + " with
employee ID " + employeeId + ".");
}
}

// Example usage
public class Main {
public static void main(String[] args) {
// Creating instances of the concrete classes
Student student = new Student("Ignatius", 19, "2022390013");
Teacher teacher = new Teacher("Wahid", 29, "123456");

// Invoking the introduce method


student.introduce();
teacher.introduce();
}
}
4. Diamond

// Java program to Print Diamond Star Pattern


// Using do-while loop

// Importing input output classes


import java.io.*;

// Main class
public class GFG {

// Main driver method


public static void main(String[] args)
{
// Declaring and initializing variables

// Variable initialized to the row where max star


// should be there as after that they decreases to
// give diamond pattern
int number = 7;

// Diamond starting with single star in first row,so


// initialized
int m = 1;

// Columnar printing
int n;

// Outer loop 1
// Prints the first half diamond
do {
n = 1;

// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");

// Condition for inside do-while loop 1


while (++n <= number - m + 1);
// Now
n = 1;

// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false

do {

// Print star
System.out.print("*");
}

// Condition for inner do-while loop 2


while (++n <= m * 2 - 1);

// A new row requires a new line


System.out.println();

// Condition for outer do-while loop 1


while (++m <= number);

// Now we are done with printing the upper half


// diamond.

// Note: Not to print the bottom row again in lower


// half diamond printing Hence variable to be
// initialized should one lesser than number
m = number - 1;

// Outer loop 2
// Prints the second half diamond
do {
n = 1;

// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");

} while (++n <= number - m + 1);


n = 1;

// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Prints star
System.out.print("*");

} while (++n <= m * 2 - 1);

// By now done with one row of lower diamond


// printing so a new line is required
System.out.println();

// Condition for outer do-while loop 2


while (--m > 0);
}
}
5. Initial Large Block Letter

public class InitialBlockLetter {

public static void main(String[] args) {

// Replace 'C' with the initial you want to display

char initial = 'C';

// Define the size of the block letter

int blockSize = 5;

// Create the block letter

createBlockLetter(initial, blockSize);

// Method to create the block letter

private static void createBlockLetter(char initial, int size) {

// Define the patterns for each letter (you can extend this for more letters)

String[] blockPatterns = {

" ### ",

" # # ",

"# #",

"# #",

"# #",

" # # ",

" ### "

};

// Get the index for the given initial (assuming A-Z for simplicity)
int index = initial - 'A';

// Check if the initial is within A-Z range

if (index >= 0 && index < blockPatterns.length) {

// Display the block letter

for (int i = 0; i < blockPatterns[index].length(); i++) {

for (int j = 0; j < size; j++) {

System.out.print(blockPatterns[index].charAt(i));

System.out.println();

} else {

// Display an error message if the initial is not within A-Z range

System.out.println("Invalid initial. Please use a letter from A to Z.");

You might also like