Create a GPA Calculator in Java - Full Tutorial with Source
Vložit
- čas přidán 26. 07. 2024
- Complete Java course: codingwithjohn.thinkific.com/...
Full source code for this GPA Calculator here:
codingwithjohn.com/gpa-calcul...
Building a full GPA calculator in Java! Starting from scratch, beginning to end, and coding live in this full lesson!
Learn or improve your Java by watching it being coded live!
Hey, I'm John! I'm a Lead Java Software Engineer who has been in the industry for over a decade.
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com
This is the best conding related channel I've ever stumbled upon, bro! Hat's off! Keep em coming!!
I learned a lot from this video thanks for the clear presentation
Congratulations on the video. I'm from Brazil, I'll watch all your videos :)
Good interview tip 31:22, thank you... Its cool to know that this is your first video every....☺
great work .sir kindly make its GUI as soon as possible thanks
Really loved the video..if you could do one to make GUI GPA calculator..now that that would be awesome
Yeah that would be the next big thing
This was fun.
Can we make it into a GUI type of calculator too?
Love your videos ! Why do we need to create a variable creditString but not set that as Interger at the first place ? Is it to allow us to use parseInt in the try...catch block ? Thanks
thank you for the video! May i ask if youre able to do a tutorial on GUI based cgpa calculator?
I haven't gotten into GUIs quite yet on the channel but have definitely had a few requests, so it may be something I get into in the future
Why are you using a wrapper class, Integer? Why not just use int?
I like the video.
It’d be great if you could provide a link to download the code.
I’m too slow to keep up with you in real time 😂
Thanks Austin! I might take the time to put them on GitHub for everyone at some point. For now, to thank you for the like and sub that helps so much, here you go!
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Total points / total credits
// points for a class = grade value * credits
// A = 4, B = 3....
Scanner scanner = new Scanner(System.in);
Integer totalPoints = 0;
Integer totalCredits = 0;
boolean moreClasses = false;
do {
Integer credits = 0;
boolean validCredits = true;
do {
validCredits = true;
System.out.println("Enter number of credits:");
String creditsString = scanner.nextLine();
try {
credits = Integer.parseInt(creditsString);
}
catch (NumberFormatException nfe){
System.out.println("Please enter a valid integer");
validCredits = false;
}
}
while (!validCredits);
boolean validGrade = true;
Integer gradeValue = 0;
String grade = "";
do {
validGrade = true;
System.out.println("Enter grade:");
grade = scanner.nextLine();
if (grade.equalsIgnoreCase("A")) {
gradeValue = 4;
} else if (grade.equalsIgnoreCase("B")) {
gradeValue = 3;
} else if (grade.equalsIgnoreCase("C")) {
gradeValue = 2;
} else if (grade.equalsIgnoreCase("D")) {
gradeValue = 1;
} else if (grade.equalsIgnoreCase("F")) {
gradeValue = 0;
} else {
System.out.println("You didn't enter a valid grade :(");
validGrade = false;
}
}
while (!validGrade);
Integer points = gradeValue * credits;
totalPoints += points;
totalCredits += credits;
System.out.println("Would you like to enter another class? (Y/N)");
String moreClassesString = scanner.nextLine();
moreClasses = moreClassesString.equalsIgnoreCase("Y");
}
while (moreClasses);
DecimalFormat df = new DecimalFormat("0.00");
Double gpa = Double.valueOf(totalPoints) / Double.valueOf(totalCredits);
System.out.println("Credits: " + totalCredits);
System.out.println("Points: " + totalPoints);
System.out.println("GPA: " + df.format(gpa));
scanner.close();
}
}
@@CodingWithJohn
That’s awesome man thanks.
I’m already subbed. I hope you keep posting Java vids. I like the way you talk through the code as you’re working the problems out.
@@austin8034 Thanks for the words of encouragement!
does it matter what build system I choose? IntelliJ, Maven, Gradle
Yes, each build system has its pros and cons, mainly speed and ease of use. I would suggest using Maven when passing tutorials (because there's most tutorials for it so there's less probability you would get stuck), and using Gradle in real projects (because Gradle is faster to run and also faster to set up and control if you know what you're doing).
32:06 code " 1 1A 2B " is a cool reference of self-destruct sequence 2 code of the USS Enterprise from Star Trek (the golden age of great movies)
System.out.printf("gpa: %.2f", gpa);
import java.awt.*;
import javax.swing.*;
public class File {
public static void main(String[] args) {
//
// 1.frame add
JFrame f =new JFrame();
f.setTitle("CalCulator");
f.setSize(350,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setLayout(null);
//2.add 3 panels
JPanel p1=new JPanel();
p1.setBounds(0,0,350,50);
p1.setBackground(Color.WHITE);
JPanel p2=new JPanel();
p2.setBounds(0,50,200,350);
p2.setBackground(Color.DARK_GRAY);
JPanel p3=new JPanel();
p3.setBounds(200,50,150,350);
p3.setBackground(Color.orange);
f.add(p1);
f.add(p2);
f.add(p3);
//3.txtfield add in panel 1
JTextField txt = new JTextField();
txt.setBounds(10,10,120,100);
txt.setBackground(Color.red);
p1.add(txt);
//4. grid in panel2
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.setLayout(new GridLayout(3,3));
//5. extra buttons in panel 3
JButton b1 = new JButton("+");
JButton b2 = new JButton("-");
JButton b3 = new JButton("*");
JButton b4 = new JButton("/");
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.setLayout(new FlowLayout(FlowLayout.LEFT,50,25));
f.setVisible(true);
}
}