Skip to content

Commit f4e8fc3

Browse files
committed
README.md
1 parent c459ce7 commit f4e8fc3

File tree

2 files changed

+309
-3
lines changed

2 files changed

+309
-3
lines changed

README.md

Lines changed: 309 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@
5252

5353
## Types:
5454

55-
- Primitive: for storing simple values, like numbers, characters, booleans
56-
![preview](preview/preview-4.png)
55+
- Primitive: for storing simple values, like numbers, characters, booleans.
56+
57+
![preview](preview/preview-4.png)
5758

5859
- How primitive types are stored in memory.
5960

@@ -288,7 +289,7 @@
288289
}
289290
```
290291

291-
## Mortgage Calculator:
292+
## Mortgage Calculator - v1.0:
292293

293294
```bash
294295
public class Main {
@@ -405,3 +406,308 @@
405406
}
406407
}
407408
```
409+
410+
## Switch Statements:
411+
412+
```bash
413+
public class Main {
414+
public static void main(String[] args) {
415+
String role = "admin";
416+
switch (role) {
417+
case "admin":
418+
System.out.println("You're an admin");
419+
break;
420+
case "moderator":
421+
System.out.println("You're a moderator");
422+
break;
423+
default:
424+
System.out.println("You're a guest");
425+
}
426+
427+
if (role == "admin") {
428+
System.out.println("You're an admin");
429+
} else if (role == "moderator") {
430+
System.out.println("You're a moderator");
431+
} else {
432+
System.out.println("You're a guest");
433+
}
434+
}
435+
}
436+
```
437+
438+
## Exercise: FizzBuzz:
439+
440+
```bash
441+
public class Main {
442+
public static void main(String[] args) {
443+
Scanner scanner = new Scanner(System.in);
444+
System.out.print("Number: ");
445+
int number = scanner.nextInt();
446+
447+
boolean moduloFive = (number % 5) == 0;
448+
boolean moduloThree = (number % 3) == 0;
449+
450+
if (moduloFive && moduloThree) {
451+
System.out.println("FizzBuzz");
452+
} else if (moduloFive) {
453+
System.out.println("Fizz");
454+
} else if (moduloThree) {
455+
System.out.println("Buzz");
456+
} else {
457+
System.out.println(number);
458+
}
459+
}
460+
}
461+
```
462+
463+
## Loops in Java:
464+
465+
```bash
466+
public class Main {
467+
public static void main(String[] args) {
468+
Scanner scan = new Scanner(System.in);
469+
/*FOR LOOPS*/
470+
for (int i = 0; i < 5; i++) {
471+
System.out.println("Hello, Java!");
472+
}
473+
/*WHILE LOOPS: USED WHEN DON'T KNOW HOW MANY TIMES WE NEED TO EXECUTE CODE*/
474+
int i = 0;
475+
while (i < 5) {
476+
System.out.println("Hello, Java!");
477+
i++;
478+
}
479+
480+
String input = "";
481+
while (!input.equals("quit")) {
482+
System.out.print("Input: ");
483+
input = scan.next().toLowerCase(Locale.ROOT);
484+
System.out.println(input);
485+
}
486+
/*DO-WHILE LOOPS: SIMILAR TO WHILE-LOOP BUT IT EXECUTES ATLEAST 1 TIME*/
487+
do {
488+
System.out.print("Input: ");
489+
input = scan.next().toLowerCase(Locale.ROOT);
490+
System.out.println(input);
491+
} while (!input.equals("quit"));
492+
}
493+
}
494+
```
495+
496+
## Break & Continue Keywords:
497+
498+
```bash
499+
public class Main {
500+
public static void main(String[] args) {
501+
Scanner scan = new Scanner(System.in);
502+
String input = "";
503+
504+
while (true) {
505+
System.out.print("Input: ");
506+
input = scan.next().toLowerCase(Locale.ROOT);
507+
if (input.equals("pass")) {
508+
continue;/*moves control to the beginning of the loop*/
509+
}
510+
if (input.equals("quit")) {
511+
break;/*break out of the loop*/
512+
}
513+
System.out.println(input);
514+
}
515+
}
516+
}
517+
```
518+
519+
## Mortgage Calculator - v2.0:
520+
521+
```bash
522+
public class Main {
523+
public static void main(String[] args) {
524+
final byte MONTHS_IN_YEAR = 12;
525+
final byte PERCENT = 100;
526+
527+
int principal;
528+
float annualInterest;
529+
float monthlyInterest;
530+
byte years;
531+
int numberOfPayments;
532+
533+
Scanner scanner = new Scanner(System.in);
534+
535+
while (true) {
536+
System.out.print("Principal ($1K - $1M): ");
537+
principal = scanner.nextInt();
538+
if (principal >= 1000 && principal <= 1_000_000) {
539+
break;
540+
}
541+
System.out.println("Enter a value between $1000 and $1,000,000");
542+
}
543+
544+
while (true) {
545+
System.out.print("Annual Interest Rate (1% - 30%): ");
546+
annualInterest = scanner.nextFloat();
547+
if (annualInterest >= 1 && annualInterest <= 30) {
548+
monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
549+
break;
550+
}
551+
System.out.println("Enter a value between 1 and 30");
552+
}
553+
554+
while (true) {
555+
System.out.print("Period (1 Years- 30 Years): ");
556+
years = scanner.nextByte();
557+
if (years >= 1 && years <= 30) {
558+
numberOfPayments = years * MONTHS_IN_YEAR;
559+
break;
560+
}
561+
System.out.println("Enter a value between 1 and 30");
562+
563+
}
564+
/*calculating mortgage*/
565+
double mortgage = principal * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments) / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
566+
567+
System.out.println("Mortgage: " + NumberFormat.getCurrencyInstance().format(mortgage));
568+
}
569+
}
570+
```
571+
572+
## Creating Methods:
573+
574+
```bash
575+
public class Main {
576+
public static void main(String[] args) {
577+
String message = greetUser("Avinash", "Kumar");
578+
System.out.println(message);
579+
}
580+
581+
public static String greetUser(String firstName, String lastName){
582+
return "Hello " + firstName + " " + lastName;
583+
}
584+
}
585+
```
586+
587+
## Mortgage Calculator - v3.0:
588+
589+
```bash
590+
public class Main {
591+
public static void main(String[] args) {
592+
int principal = (int) readNumber("Principal ($1K - $1M): ", 1000, 1_000_000);
593+
float annualInterest = (float) readNumber("Annual Interest Rate (1% - 30%): ", 1, 30);
594+
byte years = (byte) readNumber("Period (1 Years- 30 Years): ", 1, 30);
595+
596+
double mortgage = calculateMortgage(principal, annualInterest, years);
597+
System.out.println("Mortgage: " + NumberFormat.getCurrencyInstance().format(mortgage));
598+
}
599+
600+
public static double readNumber(String prompt, double min, double max) {
601+
Scanner scanner = new Scanner(System.in);
602+
double value;
603+
while (true) {
604+
System.out.print(prompt);
605+
value = scanner.nextDouble();
606+
if (value >= min && value <= max) {
607+
break;
608+
}
609+
System.out.println("Enter a value between " + min + " and " + max);
610+
}
611+
return value;
612+
}
613+
614+
public static double calculateMortgage(int principal, float annualInterest, byte years) {
615+
final byte MONTHS_IN_YEAR = 12;
616+
final byte PERCENT = 100;
617+
618+
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
619+
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);
620+
621+
return principal * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments) / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
622+
}
623+
}
624+
```
625+
626+
## Mortgage Calculator - Clean Code:
627+
628+
```bash
629+
public class Main {
630+
final static byte MONTHS_IN_YEAR = 12;
631+
final static byte PERCENT = 100;
632+
633+
public static void main(String[] args) {
634+
int principal = (int) readNumber("Principal ($1K - $1M): ", 1000, 1_000_000);
635+
float annualInterest = (float) readNumber("Annual Interest Rate (1% - 30%): ", 1, 30);
636+
byte years = (byte) readNumber("Period (1 Years- 30 Years): ", 1, 30);
637+
638+
printMortgage(principal, annualInterest, years);
639+
640+
printPaymentSchedule(principal, annualInterest, years);
641+
}
642+
643+
private static void printPaymentSchedule(int principal, float annualInterest, byte years) {
644+
System.out.println();
645+
System.out.println("PAYMENTS SCHEDULE:");
646+
System.out.println("------------------");
647+
for (short month = 1; month <= years * MONTHS_IN_YEAR; month++) {
648+
double balance = calculateBalance(principal, annualInterest, years, month);
649+
System.out.println(NumberFormat.getCurrencyInstance().format(balance));
650+
}
651+
}
652+
653+
private static void printMortgage(int principal, float annualInterest, byte years) {
654+
System.out.println();
655+
System.out.println("MORTGAGE");
656+
System.out.println("--------");
657+
double mortgage = calculateMortgage(principal, annualInterest, years);
658+
System.out.println("Monthly Payments: " + NumberFormat.getCurrencyInstance().format(mortgage));
659+
}
660+
661+
public static double readNumber(String prompt, double min, double max) {
662+
Scanner scanner = new Scanner(System.in);
663+
double value;
664+
while (true) {
665+
System.out.print(prompt);
666+
value = scanner.nextDouble();
667+
if (value >= min && value <= max) {
668+
break;
669+
}
670+
System.out.println("Enter a value between " + min + " and " + max);
671+
}
672+
return value;
673+
}
674+
675+
public static double calculateBalance(int principal, float annualInterest, byte years, short numberOfPaymentsMade) {
676+
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
677+
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);
678+
679+
double balance = principal * (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade)) / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
680+
681+
return balance;
682+
}
683+
684+
public static double calculateMortgage(int principal, float annualInterest, byte years) {
685+
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
686+
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);
687+
688+
return principal * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments) / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
689+
}
690+
}
691+
```
692+
693+
## Refactoring
694+
695+
- Changing the structure of the code without changing its behaviour.
696+
- Keep your methods short
697+
- Extract repetitive patterns
698+
- Extract highly related statements
699+
700+
## Types of Errors
701+
702+
- Compile - Time Errors
703+
- Run - Time Errors
704+
705+
## Running a Java Archive (.jar) in local:
706+
707+
```bash
708+
java -jar file-name.jar
709+
```
710+
711+
## Thank You!
712+
713+
![preview](preview/preview-8.png)

preview/preview-8.png

191 KB
Loading

0 commit comments

Comments
 (0)