0% found this document useful (0 votes)
6 views4 pages

Code

The document contains multiple Java code snippets demonstrating various ways to print welcome messages and formatted output. It includes examples of using string concatenation, formatting with printf, and handling user input with a Scanner. Additionally, it showcases complex expressions and variable types in Java.

Uploaded by

Muhammad Daniyal
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)
6 views4 pages

Code

The document contains multiple Java code snippets demonstrating various ways to print welcome messages and formatted output. It includes examples of using string concatenation, formatting with printf, and handling user input with a Scanner. Additionally, it showcases complex expressions and variable types in Java.

Uploaded by

Muhammad Daniyal
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/ 4

Code: A1

public class WelcomeMessage {


public static void main(String[] args) {
System.out.println("Well Come to Object Oriented Programming");
}
}
Code: A2
public class WelcomeMessage {
public static void main(String[] args) {
String s = "Well Come to Object Oriented Programming";
System.out.println(s);
}
}
Code: A3
public class WelcomeMessage {
public static void main(String[] args) {
String s = "Well Come to \nObject Oriented Programming";
System.out.println(s);
}
}
Code: A4
public class WelcomeMessage {
public static void main(String[] args) {
String s = "Well Come to \nObject Oriented Programming";
System.out.println(“%s”, s);
}
}
Code: A5
public class WelcomeMessage {
public static void main(String[] args) {
String s1 = "Well Come to” ;
String s2 = “Object Oriented Programming";
System.out.print(s1);
System.out.print(s2);

}
}
Code: A6
public class WelcomeMessage {
public static void main(String[] args) {
String s1 = "Well Come to” ;
String s2 = “Object Oriented Programming";
System.out.println(s1);
System.out.print(s2);

}
}
Code: A7
public class WelcomeMessage {
public static void main(String[] args) {
String s1 = "Well Come to” ;
String s2 = “Object Oriented Programming";
System.out.println(s1+s2);

}
}
Code: A8
public class WelcomeMessage {
public static void main(String[] args) {
String s1 = "Well Come to” ;
String s2 = “Object Oriented Programming";
System.out.printf(“%s\n%s”,s1+s2);

}
}
Code: A9
public class WelcomeMessage {
public static void main(String[] args) {

String s1 = "Well Come to" ;


String s2 = "Object Oriented Programming";
System.out.printf("%40s\n%40s",s1,s2);
}
}
Code: A10
public class WelcomeMessage {
public static void main(String[] args) {

String s1 = "Well Come to" ;


String s2 = "Object Oriented Programming";
System.out.printf("% -40s\n% -40s",s1,s2);
}
}
Code: A11
public class WelcomeMessage {
public static void main(String[] args) {
public static void main(String[] args) {

String s1 = "Well Come to" ;


String s2 = "Object Oriented Programming", space = " ";
System.out.printf("%40s\n%8s%40s",s1,space,s2);

}
}
Code: A12
public class test1 {

public static void main(String[] args) {

String nameLabel = "Name";


String ageLabel = "Age";
String salaryLabel = "Salary";
String nameValue = "John Doe";
String ageValue = "30";
String salaryValue = "$50,000";

System.out.printf("%10s: %-10s\n", nameLabel, nameValue);


System.out.printf("%10s: %-10s\n", ageLabel, ageValue);
System.out.printf("%10s: %-10s\n", salaryLabel, salaryValue);
}
}
Output:
Name: John Doe
Age: 30
Salary: $50,000
Code: A13
public class test1 {

public static void main(String[] args) {

String nameLabel = "Name";


String ageLabel = "Age";
String salaryLabel = "Salary";
String marriedLabel = "Married ";

String nameValue = "John Doe";


int ageValue = 30;
float salaryValue = (float) 50000.12341234;
boolean mariageValue = true;
System.out.printf("%10s: %-10s\n", nameLabel, nameValue);
System.out.printf("%10s: %-10d\n", ageLabel, ageValue);
System.out.printf("%10s: %-10f\n", salaryLabel, salaryValue);
System.out.printf("%10s: %-10b",marriedLabel,mariageValue);
}
}
Output:
Name: John Doe
Age: 30
Salary: 50000.1234
Married: true
Code: A14
public class ComplexExpressionExample {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = 30;
boolean result = (x > y && y != z) || (x == z);
System.out.println("The result of the complex expression is: " + result);
}
}
Code: A15
import java.util.Scanner;

public class test1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the first integer: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scanner.nextInt();
double average = (num1 + num2) / 2.0;
System.out.println("The average of " + num1 + " and " + num2 + " is: " + average);
scanner.close();
}
}

You might also like