0% found this document useful (0 votes)
12 views9 pages

Java Program Compilation Guide

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)
12 views9 pages

Java Program Compilation Guide

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

1. Lincoln2.

java

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


[Link]("My name is:");
[Link]("Ignatius Timothy Bonario");}}

2. [Link]

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

// [Link] Java Foundations

//

// Demonstrates another valid program that is poorly formatted.

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

public class

Lincoln3

{
public

static

void

main

String

[]

args )

[Link] (

"My name is:" )

; [Link]

"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) {
[Link] = name;
[Link] = 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);
[Link] = studentId;
}

// Implementation of the abstract method


@Override
public void introduce() {
[Link]("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);
[Link] = employeeId;
}

// Implementation of the abstract method


@Override
public void introduce() {
[Link]("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


[Link]();
[Link]();
}
}
4. Diamond

// Java program to Print Diamond Star Pattern


// Using do-while loop

// Importing input output classes


import [Link].*;

// 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
[Link](" ");

// 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
[Link]("*");
}

// Condition for inner do-while loop 2


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

// A new row requires a new line


[Link]();

// 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
[Link](" ");

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


n = 1;

// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Prints star
[Link]("*");

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

// By now done with one row of lower diamond


// printing so a new line is required
[Link]();

// 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 < [Link]) {

// Display the block letter

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

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

[Link](blockPatterns[index].charAt(i));

[Link]();

} else {

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

[Link]("Invalid initial. Please use a letter from A to Z.");

You might also like