Skip to content

Commit 2788f8c

Browse files
authored
Adding more java programs
1 parent a040e40 commit 2788f8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+3501
-0
lines changed

Account.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import java.util.Scanner;
3+
4+
public class Account
5+
{
6+
private String name;
7+
private double balance;
8+
9+
// default constructor.
10+
public Account()
11+
{
12+
name = "";
13+
balance = 0.0;
14+
}
15+
16+
// parametrized constructor.
17+
public Account(String name, double balance)
18+
{
19+
this.name = name;
20+
if (balance > 0.0)
21+
{
22+
this.balance = balance;
23+
}
24+
}
25+
26+
// deposite amount method.
27+
public void depositAmount(double amount)
28+
{
29+
if (amount > 0.0)
30+
{
31+
balance += amount;
32+
}
33+
}
34+
35+
// withdraw method
36+
public void withdrawn(double amount)
37+
{
38+
if (amount > 0.0 && amount <= balance)
39+
{
40+
balance -= amount;
41+
}
42+
else
43+
{
44+
System.out.println("\nWithdrawn amount exceed!.");
45+
System.out.println("Transaction failed.\n");
46+
}
47+
}
48+
49+
// balance getter method
50+
public double getBalance()
51+
{
52+
return balance;
53+
}
54+
55+
// name setter method
56+
public void setName(String name)
57+
{
58+
this.name = name;
59+
}
60+
61+
// name getter method
62+
public String getName()
63+
{
64+
return name;
65+
}
66+
67+
// main driven function.
68+
public static void main(String[] args)
69+
{
70+
var input = new Scanner(System.in);
71+
72+
var account = new Account(" ", 0);
73+
74+
System.out.print("Enter your name : ");
75+
account.setName(input.nextLine());
76+
77+
System.out.println("Your account balance is " + account.getBalance());
78+
79+
System.out.print("Enter the amount to deposit in your account : ");
80+
account.depositAmount(input.nextDouble());
81+
82+
System.out.println("Your new balance is : " + account.getBalance());
83+
84+
System.out.print("Enter amount you wish to withdraw : ");
85+
account.withdrawn(input.nextDouble());
86+
87+
System.out.println("Your new balance is : " + account.getBalance());
88+
89+
input.close();
90+
}
91+
}

AreaFinder.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
3+
// Shape abstract class
4+
abstract class Shape
5+
{
6+
// abstract method. Remember: abstract method has no body.
7+
public abstract double area();
8+
}
9+
10+
// Circle class inherited from shape class
11+
class Circle extends Shape
12+
{
13+
private double radius;
14+
15+
public Circle(double radius)
16+
{
17+
this.radius = radius;
18+
}
19+
20+
// See this annotation @Override, it is telling that this method is from parent
21+
// class Shape and is overridden here
22+
@Override
23+
public double area()
24+
{
25+
return 3.14 * radius * radius;
26+
}
27+
}
28+
29+
// Rectangle class inherited from shape class
30+
class Rectangle extends Shape
31+
{
32+
private double length;
33+
private double width;
34+
35+
Rectangle(double length, double width)
36+
{
37+
this.length = length;
38+
this.width = width;
39+
}
40+
41+
// See this annotation @Override, it is telling that this method is from parent
42+
// class Shape and is overridden here
43+
@Override
44+
public double area()
45+
{
46+
return length * width;
47+
}
48+
}
49+
50+
51+
public class AreaFinder
52+
{
53+
public static void main(String[] args)
54+
{
55+
//This will create an object of circle class
56+
Shape circle = new Circle(5.0);
57+
//This will create an object of Rectangle class
58+
Shape rectangle = new Rectangle(5.4, 5.2);
59+
60+
System.out.println("Shape of circle : " + circle.area());
61+
System.out.println("Shape of rectangle: " + rectangle.area());
62+
}
63+
}
64+

ArrayClass.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Arrays;
2+
3+
public class ArrayClass
4+
{
5+
public static void main(String[] args)
6+
{
7+
// Creating an array
8+
int[] myArray = {23, 32, 29, 72, 55};
9+
10+
// Printing the myArray using Arrays class
11+
System.out.println(Arrays.toString(myArray));
12+
13+
// Sorting the array
14+
Arrays.sort(myArray);
15+
System.out.println(Arrays.toString(myArray));
16+
}
17+
}

Arrays.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// Muhammad Naveed
3+
// Arrays demonstration.
4+
5+
import java.util.Scanner;
6+
7+
public class Arrays
8+
{
9+
public static void main(String[] args)
10+
{
11+
var s = new Scanner(System.in);
12+
// Creating new array.
13+
int[] myArray = new int[5];
14+
15+
// Taking value from user and storing in array.
16+
System.out.println("Enter the values in array : ");
17+
for (int i = 0; i < myArray.length; i++)
18+
{
19+
myArray[i] = s.nextInt();
20+
}
21+
22+
s.close();
23+
24+
// Printing the items of array.
25+
for (int values : myArray)
26+
{
27+
System.out.print(values + " ");
28+
}
29+
}
30+
}

Break.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
4+
5+
public class Break
6+
{
7+
public static void main(String[] args)
8+
{
9+
int userValue = 11;
10+
11+
// Iteration for 100 times.
12+
for (int index = 0; index < 100; index++)
13+
{
14+
// if index == userValue (11) then, break the loop.
15+
if (index == userValue)
16+
{
17+
break;
18+
}
19+
// printing the index.
20+
System.out.println(index);
21+
}
22+
}
23+
}

Comments.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
// Muhammad Naveed
3+
// Comments demonstration.
4+
5+
6+
public class Comments
7+
{
8+
/*
9+
* This is a multiline comment.
10+
* it may uses more than one lines.
11+
*/
12+
public static void main(String[] args)
13+
{
14+
// printing 'Hello world' to console.
15+
System.out.println("Hello world");
16+
}
17+
}

CountAndPrint.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
4+
class CountAndPrint implements Runnable
5+
{
6+
7+
private final String name;
8+
9+
CountAndPrint(String name)
10+
{
11+
this.name = name;
12+
}
13+
14+
/** This is what a CountAndPrint will do */
15+
@Override
16+
public void run()
17+
{
18+
for (int i = 0; i < 100; i++)
19+
{
20+
System.out.println(this.name + ": " + i);
21+
}
22+
}
23+
24+
public static void main(String[] args)
25+
{
26+
// Launching 4 parallel threads
27+
for (int i = 1; i <= 4; i++)
28+
{
29+
// `start` method will call the `run` method
30+
// of CountAndPrint in another thread
31+
new Thread(new CountAndPrint("Instance " + i)).start();
32+
}
33+
// Doing some others tasks in the main Thread
34+
for (int i = 0; i < 100; i++)
35+
{
36+
System.out.println("Main: " + i);
37+
}
38+
}
39+
}

DoWhileLoop.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
// Muhammad Naveed
4+
// Do While Loop demonstration
5+
6+
7+
public class DoWhileLoop
8+
{
9+
public static void main(String[] args)
10+
{
11+
int i = 1;
12+
13+
do
14+
{
15+
System.out.println(i);
16+
i++;
17+
}
18+
while (i != 10);
19+
}
20+
}

Elvis.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
4+
5+
6+
public class Elvis
7+
{
8+
public static int checkMax(int first, int second)
9+
{
10+
int maxNumber = (first > second) ? first : second;
11+
12+
return maxNumber;
13+
}
14+
public static void main(String[] args)
15+
{
16+
System.out.println(checkMax(23, 43));
17+
}
18+
}

Enumirations.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
4+
public class Enumirations
5+
{
6+
enum D
7+
{
8+
EAST, WEST, NORTH, SOUTH
9+
}
10+
11+
public static void main(String[] args)
12+
{
13+
System.out.println(D.valueOf("0"));
14+
}
15+
}

Equal.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
4+
public class Equal
5+
{
6+
public static void main(String[] args)
7+
{
8+
int firstUserIncome = 23_000;
9+
int secondUserIncome = 2_000_000;
10+
11+
// Equality Operator (==)
12+
if (firstUserIncome == secondUserIncome)
13+
{
14+
System.out.println("Both have same income");
15+
}
16+
17+
// Inequality Operator (!=)
18+
if (firstUserIncome != secondUserIncome)
19+
{
20+
System.out.println("Both don't have same income");
21+
}
22+
}
23+
}

ExplicitCasting.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
// Muhammad Naveed
3+
// Explicit Casting demonstration
4+
5+
6+
public class ExplicitCasting
7+
{
8+
public static void main(String[] args)
9+
{
10+
float fValue = 4.4F;
11+
int iValue = (int) fValue * 3;
12+
13+
System.out.println(iValue); // 12
14+
}
15+
}

0 commit comments

Comments
 (0)