Marc Hauschildt
Web Technologies and Computer Software Development Instructor at Kirkwood Community College in Cedar Rapids, IA.
Week 6
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
numerator = 1;
denominator = 1;
}
public String toString() {
return numerator + "/" + denominator;
}
}
Create another constructor that has two int parameters, called numerator and denominator. Assign both parameters to the instance variables.
public class Fraction {
// code omitted
public Fraction(int numerator, int denominator) {
setNumerator(numerator);
setDenominator(denominator);
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
}
public void setDenominator(int denominator) {
if(denominator == 0){
throw new ArithmeticException("cannot be zero");
}
this.denominator = denominator;
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public Fraction simplify() {
int gcd = gcd(this.numerator, this.denominator);
Fraction simplified = new Fraction(this.numerator/gcd, this.denominator/gcd);
return simplified;
}
public Fraction simplify() {
int gcd = gcd(this.numerator, this.denominator);
Fraction simplified = new Fraction(this.numerator/gcd, this.denominator/gcd);
if(simplified.numerator >= 0 && simplified.denominator <= 0
|| simplified.numerator <= 0 && simplified.denominator <= 0) {
simplified.numerator *= -1;
simplified.denominator *= -1;
}
return simplified;
}
public String mixedNumber() {
Fraction simplified = this.simplify();
if(simplified.denominator == 1) {
return Integer.toString(simplified.numerator);
}
return simplified.toString();
}
public String mixedNumber() {
Fraction simplified = this.simplify();
if(simplified.denominator == 1) {
return Integer.toString(simplified.numerator);
} else if(simplified.numerator == 0) {
return "0";
} else if(simplified.numerator > simplified.denominator) {
int wholeNumber = simplified.numerator / simplified.denominator;
int remainder = simplified.numerator % simplified.denominator;
return wholeNumber + " " + remainder + "/" + simplified.denominator;
}
return simplified.toString();
}
public String mixedNumber() {
Fraction simplified = this.simplify();
if(simplified.denominator == 1) {
return Integer.toString(simplified.numerator);
} else if(simplified.numerator == 0) {
return "0";
} else if(simplified.numerator > simplified.denominator) {
int wholeNumber = simplified.numerator / simplified.denominator;
int remainder = simplified.numerator % simplified.denominator;
return wholeNumber + " " + remainder + "/" + simplified.denominator;
} else if(simplified.numerator < 0 && Math.abs(simplified.numerator) > simplified.denominator) {
int wholeNumber = simplified.numerator / simplified.denominator;
int remainder = Math.abs(simplified.numerator) % simplified.denominator;
return wholeNumber + " " + remainder + "/" + simplified.denominator;
}
return simplified.toString();
}
public String add(Fraction other) {
Fraction result = new Fraction();
result.setNumerator(this.numerator * other.denominator + other.numerator * this.denominator);
result.setDenominator(this.denominator * other.denominator);
return String.format("%s + %s = %s", this.mixedNumber(), other.mixedNumber(), result.mixedNumber());
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fraction Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container my-4">
<h1>Fraction Calculator</h1>
<p class="lead">Enter two fractions and press submit to calculate the sum.</p>
<form>
<div class="row">
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control">
</div>
</div>
<div class="col-1 d-flex justify-content-center display-3">
<span>+</span>
</div>
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value="/fraction")
public class FractionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
}
}
<form method="post" action="fraction">
name="num1"
name="den1"
name="num2"
name="den2"
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fraction Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container my-4">
<h1>Fraction Calculator</h1>
<p class="lead">Enter two fractions and press submit to calculate the sum.</p>
<form method="post" action="fraction">
<div class="row">
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num1">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den1">
</div>
</div>
<div class="col-1 d-flex justify-content-center display-3">
<span>+</span>
</div>
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num2">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den2">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String den1 = req.getParameter("den1");
String num2 = req.getParameter("num2");
String den2 = req.getParameter("den2");
req.setAttribute("num1", num1);
req.setAttribute("den1", den1);
req.setAttribute("num2", num2);
req.setAttribute("den2", den2);
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String num1 = (String)request.getAttribute("num1");
num1 = num1 == null ? "" : num1;
String den1 = (String)request.getAttribute("den1");
den1 = den1 == null ? "" : den1;
String num2 = (String)request.getAttribute("num2");
num2 = num2 == null ? "" : num2;
String den2 = (String)request.getAttribute("den2");
den2 = den2 == null ? "" : den2;
%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fraction Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container my-4">
<h1>Fraction Calculator</h1>
<p class="lead">Enter two fractions and press submit to calculate the sum.</p>
<form method="post" action="fraction">
<div class="row">
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num1" value="<%= num1 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den1" value="<%= den1 %>">
</div>
</div>
<div class="col-1 d-flex justify-content-center display-3">
<span>+</span>
</div>
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num2" value="<%= num2 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den2" value="<%= den2 %>">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String den1 = req.getParameter("den1");
String num2 = req.getParameter("num2");
String den2 = req.getParameter("den2");
req.setAttribute("num1", num1);
req.setAttribute("den1", den1);
req.setAttribute("num2", num2);
req.setAttribute("den2", den2);
String error = "";
Fraction f1 = new Fraction();
try {
f1.setNumerator(Integer.parseInt(num1));
} catch (NumberFormatException e) {
error += "<li>Numerator 1 is not valid</li>";
}
Fraction f2 = new Fraction();
try {
f2.setNumerator(Integer.parseInt(num2));
} catch (NumberFormatException e) {
error += "<li>Numerator 2 is not valid</li>";
}
if(!error.equals("")) {
req.setAttribute("error", "<ul>" + error + "</ul>");
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String num1 = (String)request.getAttribute("num1");
num1 = num1 == null ? "" : num1;
String den1 = (String)request.getAttribute("den1");
den1 = den1 == null ? "" : den1;
String num2 = (String)request.getAttribute("num2");
num2 = num2 == null ? "" : num2;
String den2 = (String)request.getAttribute("den2");
den2 = den2 == null ? "" : den2;
String error = (String)request.getAttribute("error");
error = error == null ? "" : error;
%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fraction Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container my-4">
<h1>Fraction Calculator</h1>
<p class="lead">Enter two fractions and press submit to calculate the sum.</p>
<form method="post" action="fraction">
<div class="row">
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num1" value="<%= num1 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den1" value="<%= den1 %>">
</div>
</div>
<div class="col-1 d-flex justify-content-center display-3">
<span>+</span>
</div>
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num2" value="<%= num2 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den2" value="<%= den2 %>">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div style="color: red;"><%= error %></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String den1 = req.getParameter("den1");
String num2 = req.getParameter("num2");
String den2 = req.getParameter("den2");
req.setAttribute("num1", num1);
req.setAttribute("den1", den1);
req.setAttribute("num2", num2);
req.setAttribute("den2", den2);
String error = "";
Fraction f1 = new Fraction();
try {
f1.setNumerator(Integer.parseInt(num1));
} catch (NumberFormatException e) {
error += "<li>Numerator 1 is not valid</li>";
}
try {
f1.setDenominator(Integer.parseInt(den1));
} catch (NumberFormatException e) {
error += "<li>Denominator 1 is not valid</li>";
} catch (ArithmeticException e) {
error += "<li>Denominator 1 cannot be zero</li>";
}
Fraction f2 = new Fraction();
try {
f2.setNumerator(Integer.parseInt(num2));
} catch (NumberFormatException e) {
error += "<li>Numerator 2 is not valid</li>";
}
try {
f2.setDenominator(Integer.parseInt(den2));
} catch (NumberFormatException e) {
error += "<li>Denominator 2 is not valid</li>";
} catch (ArithmeticException e) {
error += "<li>Denominator 2 cannot be zero</li>";
}
if(!error.equals("")) {
req.setAttribute("error", "<ul>" + error + "</ul>");
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String den1 = req.getParameter("den1");
String num2 = req.getParameter("num2");
String den2 = req.getParameter("den2");
req.setAttribute("num1", num1);
req.setAttribute("den1", den1);
req.setAttribute("num2", num2);
req.setAttribute("den2", den2);
String error = "";
Fraction f1 = new Fraction();
try {
f1.setNumerator(Integer.parseInt(num1));
} catch (NumberFormatException e) {
error += "<li>Numerator 1 is not valid</li>";
}
try {
f1.setDenominator(Integer.parseInt(den1));
} catch (NumberFormatException e) {
error += "<li>Denominator 1 is not valid</li>";
} catch (ArithmeticException e) {
error += "<li>Denominator 1 cannot be zero</li>";
}
Fraction f2 = new Fraction();
try {
f2.setNumerator(Integer.parseInt(num2));
} catch (NumberFormatException e) {
error += "<li>Numerator 2 is not valid</li>";
}
try {
f2.setDenominator(Integer.parseInt(den2));
} catch (NumberFormatException e) {
error += "<li>Denominator 2 is not valid</li>";
} catch (ArithmeticException e) {
error += "<li>Denominator 2 cannot be zero</li>";
}
if(!error.equals("")) {
req.setAttribute("error", "<ul>" + error + "</ul>");
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
return;
}
String result = f1.add(f2);
req.setAttribute("result", result);
req.getRequestDispatcher("fraction.jsp").forward(req, resp);
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String num1 = (String)request.getAttribute("num1");
num1 = num1 == null ? "" : num1;
String den1 = (String)request.getAttribute("den1");
den1 = den1 == null ? "" : den1;
String num2 = (String)request.getAttribute("num2");
num2 = num2 == null ? "" : num2;
String den2 = (String)request.getAttribute("den2");
den2 = den2 == null ? "" : den2;
String error = (String)request.getAttribute("error");
error = error == null ? "" : error;
String result = (String)request.getAttribute("result");
result = result == null ? "" : result;
%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fraction Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container my-4">
<h1>Fraction Calculator</h1>
<p class="lead">Enter two fractions and press submit to calculate the sum.</p>
<form method="post" action="fraction">
<div class="row">
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num1" value="<%= num1 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den1" value="<%= den1 %>">
</div>
</div>
<div class="col-1 d-flex justify-content-center display-3">
<span>+</span>
</div>
<div class="col-1">
<div class="form-group mb-2 pb-2 border-bottom border-5">
<input type="text" class="form-control" name="num2" value="<%= num2 %>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="den2" value="<%= den2 %>">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div style="color: red;"><%= error %></div>
<div style="color: green;"><%= result %></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
By Marc Hauschildt
Web Technologies and Computer Software Development Instructor at Kirkwood Community College in Cedar Rapids, IA.