Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8749a8

Browse files
authored
Add files via upload
1 parent f76c042 commit f8749a8

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

‎Java_Beginner_Projects/Cal.java‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import javax.swing.JOptionPane;
2+
//A dialog calculator program.
3+
public class Cal {
4+
int input; // Class variable
5+
double x, y, z; // Class variable
6+
7+
public void Menu(){//Class Menu method
8+
JOptionPane.showMessageDialog(null, "A Simple Calculator using Dialog." );
9+
input = Integer.parseInt(JOptionPane.showInputDialog("\t Menu: \n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Exit \n Enter an option: "));
10+
}
11+
12+
public void Add(){//Class Addition Method.
13+
JOptionPane.showMessageDialog(null, "\t Addition");
14+
x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: "));
15+
y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: "));
16+
z = x+y;
17+
JOptionPane.showMessageDialog(null, x + " + " + y + " = " + (z));
18+
}
19+
20+
public void Sub(){//Class Subtraction method
21+
JOptionPane.showMessageDialog(null, "\t Subtraction");
22+
x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: "));
23+
y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: "));
24+
z = x-y;
25+
JOptionPane.showMessageDialog(null, x + " - " + y + " = " + (z));
26+
}
27+
28+
public void Mul(){//Class Multiplication method
29+
JOptionPane.showMessageDialog(null, "\t Addition");
30+
x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: "));
31+
y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: "));
32+
z = x*y;
33+
JOptionPane.showMessageDialog(null, x + " x " + y + " = " + (z));
34+
}
35+
36+
public void Div(){//Class Division method.
37+
JOptionPane.showMessageDialog(null, "\t Addition");
38+
x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: "));
39+
y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: "));
40+
z = x/y;
41+
//Validating the division method, incase user input for y = 0;
42+
if(y ==0){
43+
JOptionPane.showMessageDialog(null, "Division by zero is invalid!");
44+
}
45+
else{
46+
JOptionPane.showMessageDialog(null, x + " / " + y + " = " + (z));
47+
}
48+
}
49+
public void exit() {//A class method to quit the program.
50+
JOptionPane.showMessageDialog(null, "You closed the program, goodbye!");
51+
}
52+
public static void main(String[] args) {
53+
int next;
54+
55+
do{//A do-while loop for program reusability.
56+
57+
Cal h = new Cal();//An object for the class.
58+
h.Menu();//Calling te menu Menu.
59+
//A list of conditions to validate user inputs for menu options.
60+
if (h.input==1){
61+
h.Add();
62+
}
63+
else if (h.input==2){
64+
h.Sub();
65+
}
66+
else if (h.input==3){
67+
h.Mul();
68+
}
69+
else if (h.input==4){
70+
h.Div();
71+
}
72+
else if (h.input==5) {
73+
h.exit();
74+
}
75+
else{//An else statement for error handling.
76+
JOptionPane.showMessageDialog(null, "Your input was invalid. Please try again.");
77+
}
78+
79+
//Global variable for program continuation.
80+
next = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want to continue: (1.Yes / 2.No)"));
81+
if(next == 2){h.exit();}//for quitting when a user choise is 2(No).
82+
83+
}while(next == 1);//Continue while user input is yes for next.
84+
}
85+
}
2.45 KB
Binary file not shown.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.util.Scanner;// A library that permits user input in java.
2+
public class Calculator {// A simple calculator program.
3+
4+
Scanner scanner = new Scanner(System.in);//A variable that holds user inputs.
5+
6+
public void Menu(int Option){// A Menu method.
7+
System.out.println("\t A Simple Calculator program \n\t--Menu-- \n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Exit");
8+
System.out.print("Enter an option: ");
9+
10+
}
11+
//The Addition method
12+
public int Add(){
13+
System.out.println("\n \tAddition...");
14+
System.out.println("Enter your first number: ");
15+
int a = scanner.nextInt();
16+
System.out.println("Enter your second number: ");
17+
int b = scanner.nextInt();
18+
System.out.println("Answer = " + (a + b));
19+
return a + b;
20+
}
21+
//The subtraction method.
22+
public int Sub(){
23+
System.out.println("\n \tSubtraction...");
24+
System.out.println("Enter your first number: ");
25+
int a = scanner.nextInt();
26+
System.out.println("Enter your second number: ");
27+
int b = scanner.nextInt();
28+
System.out.println("Answer = " + (a - b));
29+
return a - b;
30+
}
31+
32+
// The multiplication method.
33+
public int Mul(){
34+
System.out.println("\n \tMultiplication...");
35+
System.out.println("Enter your first number: ");
36+
int a = scanner.nextInt();
37+
System.out.println("Enter your second number: ");
38+
int b = scanner.nextInt();
39+
System.out.println("Answer = " + (a * b));
40+
return a * b;
41+
}
42+
43+
//The division method.
44+
public void Div(){
45+
System.out.println("\n \tDivision...");
46+
System.out.println("Enter your first number: ");
47+
int a = scanner.nextInt();
48+
System.out.println("Enter your second number: ");
49+
int b = scanner.nextInt();
50+
if (b==0){
51+
String zero ="Division by zero is invalid";
52+
System.err.println(zero);
53+
}
54+
else{
55+
System.out.println("Answer = " + (a / b));
56+
}
57+
}
58+
59+
//A method for exit.
60+
public void exit(){
61+
System.out.println("Exiting...");
62+
}
63+
64+
int option;
65+
//Main Method.
66+
public void main(String [] args){
67+
option = scanner.nextInt();
68+
int c;
69+
do{
70+
Menu(option);//calling the menu method using the args option.
71+
72+
switch (option) {
73+
case 1:
74+
Add();
75+
break;
76+
case 2:
77+
Sub();
78+
break;
79+
case 3:
80+
Mul();
81+
break;
82+
case 4:
83+
Div();
84+
break;
85+
case 5:
86+
exit();
87+
break;
88+
default:
89+
System.out.println("Unknown option");
90+
break;
91+
}
92+
System.out.println("Do you want to continue? \n (1. Yes 2. No)");
93+
c = scanner.nextInt();
94+
if (c== 2){exit();}
95+
else{
96+
System.out.println("\n\n\n");
97+
}
98+
}while(c == 1);
99+
}
100+
}

‎Java_Beginner_Projects/fizzbuzz.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class fizzbuzz {
2+
public void main(String[] args){
3+
Display();
4+
5+
}
6+
7+
public void Display(){
8+
System.err.println("Welcome to the FizzBuzz: \t");
9+
for (int i = 1; i <= 100; i++) {
10+
if (i % 5 == 0 && i % 3 == 0) {
11+
System.err.println("FizzBuzz");
12+
}
13+
else if (i % 5 == 0){
14+
System.err.println("Fizz");
15+
}
16+
else if(i % 3 == 0){
17+
System.err.println("Buzz");
18+
}
19+
else{
20+
System.err.println(i);
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /