Skip to content

Commit c6d8cda

Browse files
Java Practice Programs on OOPS Concepts
0 parents  commit c6d8cda

14 files changed

+233
-0
lines changed

AddThreeNum.class

903 Bytes
Binary file not shown.

AddThreeNum.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//todo: Write a java program to add three numbers.
2+
3+
public class AddThreeNum {
4+
public static void main(String[] args) {
5+
int num1 = 10;
6+
int num2 = 15;
7+
int num3 = 25;
8+
System.out.println("The Addition of " + num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));
9+
}
10+
}

Chapter_1_PS.class

768 Bytes
Binary file not shown.

Chapter_1_PS.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//* Practice Set fof Chapter-1 of Java
2+
3+
import java.util.Scanner; //? This for reading input from the user
4+
5+
public class Chapter_1_PS {
6+
public static void main(String[] args) {
7+
// ? Create object of the scanner class
8+
Scanner sc = new Scanner(System.in);
9+
10+
// ! 1. Write a program to sum three numbers in Java.
11+
// ? Get number from the user
12+
System.out.print("Enter The Number 1 : ");
13+
float n1 = sc.nextFloat();
14+
System.out.print("Enter The Number 2 : ");
15+
float n2 = sc.nextFloat();
16+
System.out.print("Enter The Number 3 : ");
17+
float n3 = sc.nextFloat();
18+
// ? Perform sum of this three number
19+
float sum = n1 + n2 + n3;
20+
// ? Display the result
21+
System.out.println("The Sum of this three number is : " + sum);
22+
23+
// ! 2. Write a program to calculate CGPA using marks of three subjects (out of
24+
// ! 100)
25+
// ? Get the Marks of each subject from the user
26+
System.out.print("Enter The Marks of Subject 1 : ");
27+
float sub1 = sc.nextFloat();
28+
System.out.print("Enter The Marks of Subject 2 : ");
29+
float sub2 = sc.nextFloat();
30+
System.out.print("Enter The Marks of Subject 3 : ");
31+
float sub3 = sc.nextFloat();
32+
// ? Calculate the Sum of the marks
33+
float sumMarks = sub1 + sub2 + sub3;
34+
// ? Find the percentage
35+
float percentage = sumMarks / 3;
36+
// ? Find out the CGPA
37+
float CGPA = percentage / 9.5f;
38+
// ? Print all the results
39+
System.out.println("Your Total of this : " + sumMarks);
40+
System.out.println("Your Percentage is this : " + percentage);
41+
System.out.println("Your CGPA is this : " + CGPA);
42+
43+
// ! 3. Write a Java program that asks the user to enter his/her name and greets
44+
// them with “Hello <name>, have a good day” text.
45+
// ? Get the name of the user
46+
System.out.print("Enter Your Name Here : ");
47+
String name = sc.nextLine();
48+
// ? greets them with “Hello <name>, have a good day” text
49+
System.out.println("Hello " + name + ", have a good day.");
50+
51+
// ! 4. Write a Java program to convert Kilometers to miles.
52+
// ? Get the kilometers from the user
53+
System.out.print("Enter The Distance in kilometers : ");
54+
int km = sc.nextInt();
55+
// ? Convert the kilometers in miles
56+
float miles = km * 0.621371f;
57+
System.out.println("The kilometers is converted into miles : " + miles);
58+
59+
// ! 5. Write a Java program to detect whether a number entered by the user is
60+
// an integer or not. If it’s an integer, print “It’s An Integer”. If it’s not
61+
// ? Get the integer number from the user
62+
System.out.print("Enter The Number Here : ");
63+
boolean isInteger = sc.hasNextInt();
64+
if (isInteger) {
65+
System.out.println("Yes, This is an Integer!");
66+
} else {
67+
System.out.println("No, This is not an Integer!");
68+
}
69+
70+
sc.close();
71+
}
72+
}

Exercise_1.class

1.42 KB
Binary file not shown.

Exercise_1.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//* Write a program to calculate the percentage of a given student in the CBSE board exam. His marks from 5 subjects must be taken as input from the keyboard. (Marks are out of 100)
2+
3+
import java.util.Scanner;
4+
5+
public class Exercise_1 {
6+
public static void main(String[] args) {
7+
// ? Create instance of the Scanner class to use the inbuilt method
8+
Scanner sc = new Scanner(System.in);
9+
10+
// ? Get the Marks of 5 subjects from the user
11+
System.out.print("Enter The Marks of 1 Subject : ");
12+
float sub1 = sc.nextFloat();
13+
System.out.print("Enter The Marks of 2 Subject : ");
14+
float sub2 = sc.nextFloat();
15+
System.out.print("Enter The Marks of 3 Subject : ");
16+
float sub3 = sc.nextFloat();
17+
System.out.print("Enter The Marks of 4 Subject : ");
18+
float sub4 = sc.nextFloat();
19+
System.out.print("Enter The Marks of 5 Subject : ");
20+
float sub5 = sc.nextFloat();
21+
22+
// ? Calculate the total of the student marks
23+
float total = sub1 + sub2
24+
+ sub3 + sub4 + sub5;
25+
26+
// ? Calculate the percentage of the student marks
27+
float percentage = total / 5;
28+
29+
// ? Print the result to the user screen
30+
System.out.println("The Total of the All The Subjects is : " + total);
31+
System.out.println("The Percentage Is : " + percentage);
32+
33+
sc.close();
34+
}
35+
}

HelloWorld.class

427 Bytes
Binary file not shown.

HelloWorld.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//todo: First Java Hello World Program to print "hello, world"
2+
3+
public class HelloWorld {
4+
public static void main(String[] args) {
5+
System.out.println("Hello, world!");
6+
}
7+
}

Literals.class

638 Bytes
Binary file not shown.

Literals.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//todo: In Java There are some literals
2+
//todo: Literals means A constant value which can be assigned to the variable is called as a literal.
3+
4+
public class Literals {
5+
public static void main(String[] args) {
6+
// ? Integer literal
7+
int a = 10;
8+
System.out.println(a);
9+
10+
// ? Character Literal - You can place the character in single quote
11+
char b = 'A';
12+
System.out.println(b);
13+
14+
// ? Double Literal - You can specify the d with in the last of the number
15+
double c = 154545454d;
16+
System.out.println(c);
17+
18+
// ? Byte Literal
19+
byte d = 10;
20+
System.out.println(d);
21+
22+
// ? Float Literal - To Define float literal you need to add f to the end of the
23+
// number
24+
float e = 10.5f;
25+
System.out.println(e);
26+
27+
// ? Boolean Literal - This will store true of false (default value is false)
28+
boolean f = true;
29+
System.out.println(f);
30+
31+
// ? Short Literal - This will store short integer
32+
short g = 10;
33+
System.out.println(g);
34+
35+
// ? Long Literal - To Define long literal you need to add l to the end of the
36+
// long number
37+
long h = 555555555l;
38+
System.out.println(h);
39+
}
40+
}

Operators.class

953 Bytes
Binary file not shown.

Operators.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//* Introduction to Java Operators
2+
3+
public class Operators {
4+
public static void main(String[] args) {
5+
6+
int a = 10;
7+
int b = 2;
8+
9+
// ! 1. Arithmetic operators
10+
// ? Arithmetic operators are used to perform mathematical operations such as
11+
// addition, subtraction, multiplication, division, etc.
12+
System.out.println(a + b); // * 12
13+
System.out.println(a - b); // * 8
14+
System.out.println(a * b);// * 20
15+
System.out.println(a / b); // * 5
16+
System.out.println(a % b); // * remainder of the division (10/2=5), so it is 0
17+
18+
// ! 2. Comparison operators
19+
// ? As the name suggests, these operators are used to compare two operands
20+
System.out.println(a == b);// * false
21+
System.out.println(a != b); // * true
22+
System.out.println(a > b); // * true
23+
System.out.println(a < b); // * false
24+
System.out.println(a >= b); // * true
25+
System.out.println(a <= b); // * false
26+
27+
// ! 3. Logical operators
28+
// ? These operators determine the logic in an expression containing two or more
29+
// values of variable
30+
System.out.println(a == b && b == a); // * false
31+
System.out.println(a == b || b == a); // * true
32+
33+
// ! 4. Bitwise operators
34+
// ? Bitwise operators perform the operations on every bit of a number
35+
System.out.println(a & b); // * 2
36+
System.out.println(a | b); // * 10
37+
System.out.println(a ^ b); // * 8
38+
39+
// ! 5. Assignment operators
40+
int c = 10;
41+
c += 5; // * c is now 15
42+
c -= 5; // * c is now 10
43+
c *= 5; // * c is now 50
44+
c /= 5; // * c is now 10
45+
System.out.println(c);
46+
}
47+
}

Taking_Input.class

1.32 KB
Binary file not shown.

Taking_Input.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
3+
public class Taking_Input {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in); // ? Make the object of the Scanner class to use the method of the Scanner
6+
// ? System.in - Is use to get the input read data from the key-board
7+
8+
// ? Get two number from the user and perform the Arithmetic operation on it
9+
System.out.print("Enter Number One : ");
10+
int num1 = sc.nextInt();
11+
System.out.print("Enter Number Two : ");
12+
int num2 = sc.nextInt();
13+
14+
// ? Output the result to the user
15+
System.out.println("The Addition of " + num1 + " + " + num2 + " = " + (num1 + num2));
16+
System.out.println("The Subtraction of " + num1 + " - " + num2 + " = " + (num1 - num2));
17+
System.out.println("The Multiplication of " + num1 + " * " + num2 + " = " + (num1 * num2));
18+
System.out.println("The Division of " + num1 + " / " + num2 + " = " + (num1 / num2));
19+
20+
sc.close();
21+
}
22+
}

0 commit comments

Comments
 (0)