SlideShare a Scribd company logo
1 of 6
Download to read offline
can someone fix the errors in this code? the name needs to be FractionDemo.java
//author: Jazmine Tapia
//date: 20 February 2023
//file: FractionDemo.java
/*
Lab 13 will also be using demonstation the
the multiplication of two factors.
*/
//import statements for Scanner class
import java.util.Scanner;
// Define a public class called Fraction
public class FractionDemo
{
// Define private instance variables for the numerator and denominator
private int numerator;
private int denominator;
// Define a constructor for the Fraction class that takes a numerator and denominator as
arguments
public FractionDemo(int numerator, int denominator)
{
// Set the denominator using the setDenominator() method defined below
setDenominator(denominator);
// Set the numerator
this.numerator = numerator;
// Reduce the fraction
reduce();
// Adjust the signs of the numerator and denominator if necessary
adjustSigns();
}
// Define a getter method for the numerator
public int getNumerator()
{
return numerator;
}
// Define a setter method for the numerator
public void setNumerator(int numerator)
{
this.numerator = numerator;
}
// Define a getter method for the denominator
public int getDenominator()
{
return denominator;
}
// Define a setter method for the denominator
public void setDenominator(int denominator)
{
// Set the denominator using the setDenominator() method defined below
setDenominator(denominator);
// Reduce the fraction
reduce();
// Adjust the signs of the numerator and denominator if necessary
adjustSigns();
}
// Define a private method to set the denominator
private void setDenominator(int denominator)
{
// If the denominator is zero, throw an exception
if (denominator == 0)
{
throw new IllegalArgumentException("Denominator cannot be zero");
}
// Set the denominator
this.denominator = denominator;
}
// Define a private method to calculate the greatest common divisor of two integers
private int gcd(int a, int b)
{
if (b == 0)
{
return a;
} else {
return gcd(b, a % b);
}
}
// Define a public method to reduce the fraction
public void reduce()
{
// Calculate the greatest common divisor of the numerator and denominator
int gcd = gcd(numerator, denominator);
// Divide both the numerator and denominator by the greatest common divisor to reduce the
fraction
numerator /= gcd;
denominator /= gcd;
}
// Define a private method to adjust the signs of the numerator and denominator if necessary
private void adjustSigns()
{
// If the denominator is negative, multiply both the numerator and denominator by -1 to adjust
the signs
if (denominator < 0)
{
numerator *= -1;
denominator *= -1;
}
}
// Define a public method to add two fractions
public FractionDemo add(FractionDemo other)
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Get the numerator and denominator for the first fraction
System.out.print("Enter the numerator of the first fraction: ");
int num1 = input.nextInt();
System.out.print("Enter the denominator of the first fraction: ");
int den1 = input.nextInt();
// Get the numerator and denominator for the second fraction
System.out.print("Enter the numerator of the second fraction: ");
int num2 = input.nextInt();
System.out.print("Enter the denominator of the second fraction: ");
int den2 = input.nextInt();
// Create the two Fraction objects
Fraction f1 = new Fraction(num1, den1);
Fraction f2 = new Fraction(num2, den2);
// Display the fractions
System.out.println("f1==" + f1);
System.out.println("f2==" + f2);
// Perform arithmetic operations and display the results
System.out.println(f1 + " + " + f2 + " = " + f1.add(f2));
System.out.println(f1 + " - " + f2 + " = " + f1.subtract(f2));
System.out.println(f1 + " * " + f2 + " = " + f1.multiply(f2));
System.out.println(f1 + " / " + f2 + " = " + f1.divide(f2));
}
FractionDemo.java:110: error: illegal start of expression
public static void main(String[] args)
^
FractionDemo.java:139: error: reached end of file while parsing
}
^
2 errors
The code has to be a combination of these two codes
//import statements for Scanner class
import java.util.Scanner;
public class FractionDemo
{
public static void main(String[] args)
{
// Variables for two fractions
int num1, num2, den1, den2;
// Create Scanner object to read keyboard input
Scanner keyboard = new Scanner(System.in);
// Get integers for f1 numerator and denominator
System.out.print("Enter the numerator of the first fraction: ");
num1 = keyboard.nextInt();
System.out.print("Enter the denominator of the first fraction: ");
den1 = keyboard.nextInt();
// Get integers for f2 numerator and denominator
System.out.print("Enter the numerator of the second fraction: ");
num2 = keyboard.nextInt();
System.out.print("Enter the denominator of the second fraction: ");
den2 = keyboard.nextInt();
// Instantiate the two fractions using the input values
Fraction f1 = new Fraction(num1, den1);
Fraction f2 = new Fraction(num2, den2);
// Display the two fractions and their product
System.out.println("f1 == " + f1);
System.out.println("f2 == " + f2);
System.out.println(f1 + " * " + f2 + " == " + f1.multiply(f2));
}
}
// Define a class for fractions
class Fraction
{
private int numerator;
private int denominator;
// Constructor for Fraction class
public Fraction(int num, int den)
{
this.numerator = num;
this.denominator = den;
// Check if denominator is zero and set it to 1 if true
if (den == 0)
{
System.out.println("For Fraction: ( " + num + " / " + den + " ):");
System.out.println("Denominator cannot be zero; denominator value is being reset to one.");
this.denominator = 1; // Reset denominator to 1
System.out.println("The Fraction is now: " + this);
}
}
// Define a method for multiplying two fractions
public Fraction multiply(Fraction other)
{
int num = this.numerator * other.numerator;
int den = this.denominator * other.denominator;
return new Fraction(num, den);
}
// Override the toString() method to display the Fraction objects as strings
public String toString()
{
if (denominator == 1)
return " ( " + numerator + " ) ";
else
return " ( " + numerator + " / " + denominator + " ) ";
}
}
can someone fix the errors in this code- the name needs to be Fraction.pdf

More Related Content

Similar to can someone fix the errors in this code- the name needs to be Fraction.pdf

Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
annapurnnatextailes
 
Write a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdfWrite a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdf
leventhalbrad49439
 
JAVA Write a class called F.pdf
JAVA  Write a class called F.pdfJAVA  Write a class called F.pdf
JAVA Write a class called F.pdf
santanadenisesarin13
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
manjan6
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdf
arihantkitchenmart
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdf
archgeetsenterprises
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdf
eyeonsecuritysystems
 
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docxprog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
wkyra78
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
info706022
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
angelfragranc
 
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
exxonzone
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
sutharbharat59
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 

Similar to can someone fix the errors in this code- the name needs to be Fraction.pdf (20)

Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 
Write a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdfWrite a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdf
 
JAVA Write a class called F.pdf
JAVA  Write a class called F.pdfJAVA  Write a class called F.pdf
JAVA Write a class called F.pdf
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdf
 
Import java
Import javaImport java
Import java
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdf
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdf
 
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docxprog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
 
Functions
FunctionsFunctions
Functions
 
Lecture 11 compiler ii
Lecture 11 compiler iiLecture 11 compiler ii
Lecture 11 compiler ii
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
 
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
 
PROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docxPROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docx
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 

More from vinaythemodel

Can you fix the problem with the following code #include -iostream- #.pdf
Can you fix the problem with the following code  #include -iostream- #.pdfCan you fix the problem with the following code  #include -iostream- #.pdf
Can you fix the problem with the following code #include -iostream- #.pdf
vinaythemodel
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
vinaythemodel
 

More from vinaythemodel (20)

c) Consider the case where a sender S continuously sends frames to a r.pdf
c) Consider the case where a sender S continuously sends frames to a r.pdfc) Consider the case where a sender S continuously sends frames to a r.pdf
c) Consider the case where a sender S continuously sends frames to a r.pdf
 
Carolyn is a server at an Olive Garden restaurant and enjoys meeting n.pdf
Carolyn is a server at an Olive Garden restaurant and enjoys meeting n.pdfCarolyn is a server at an Olive Garden restaurant and enjoys meeting n.pdf
Carolyn is a server at an Olive Garden restaurant and enjoys meeting n.pdf
 
Capillary exchange- Describe how materials move out of and into capill.pdf
Capillary exchange- Describe how materials move out of and into capill.pdfCapillary exchange- Describe how materials move out of and into capill.pdf
Capillary exchange- Describe how materials move out of and into capill.pdf
 
Capillary exchange- Describe how materials move out of and into capill (1).pdf
Capillary exchange- Describe how materials move out of and into capill (1).pdfCapillary exchange- Describe how materials move out of and into capill (1).pdf
Capillary exchange- Describe how materials move out of and into capill (1).pdf
 
Can you please show the truth table as well There are three inputs r-s.pdf
Can you please show the truth table as well There are three inputs r-s.pdfCan you please show the truth table as well There are three inputs r-s.pdf
Can you please show the truth table as well There are three inputs r-s.pdf
 
Can you find the mutation (difference) between the two sequences- High.pdf
Can you find the mutation (difference) between the two sequences- High.pdfCan you find the mutation (difference) between the two sequences- High.pdf
Can you find the mutation (difference) between the two sequences- High.pdf
 
Can you fix the problem with the following code #include -iostream- #.pdf
Can you fix the problem with the following code  #include -iostream- #.pdfCan you fix the problem with the following code  #include -iostream- #.pdf
Can you fix the problem with the following code #include -iostream- #.pdf
 
Can you answer questions 30- 31 and 32 30- anterferons b- Respond be.pdf
Can you answer questions 30- 31 and 32   30- anterferons b- Respond be.pdfCan you answer questions 30- 31 and 32   30- anterferons b- Respond be.pdf
Can you answer questions 30- 31 and 32 30- anterferons b- Respond be.pdf
 
Can someone please answer this question -2 Eligibility Requirements- Y.pdf
Can someone please answer this question -2 Eligibility Requirements- Y.pdfCan someone please answer this question -2 Eligibility Requirements- Y.pdf
Can someone please answer this question -2 Eligibility Requirements- Y.pdf
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
 
Can a student attending an American university raise First Amendment o.pdf
Can a student attending an American university raise First Amendment o.pdfCan a student attending an American university raise First Amendment o.pdf
Can a student attending an American university raise First Amendment o.pdf
 
can a groundwater sample with a low redox potential could indicate a-.pdf
can a groundwater sample with a low redox potential could indicate a-.pdfcan a groundwater sample with a low redox potential could indicate a-.pdf
can a groundwater sample with a low redox potential could indicate a-.pdf
 
Calculate FIS-FST- and FIT for the population with genotype frequencie.pdf
Calculate FIS-FST- and FIT for the population with genotype frequencie.pdfCalculate FIS-FST- and FIT for the population with genotype frequencie.pdf
Calculate FIS-FST- and FIT for the population with genotype frequencie.pdf
 
C language Make a program to get utime and stime From -proc-pid-stat-.pdf
C language Make a program to get utime and stime  From -proc-pid-stat-.pdfC language Make a program to get utime and stime  From -proc-pid-stat-.pdf
C language Make a program to get utime and stime From -proc-pid-stat-.pdf
 
Calculate the Current Ratios- (Please show the formula) CompanyCurre.pdf
Calculate the Current Ratios- (Please show the formula)   CompanyCurre.pdfCalculate the Current Ratios- (Please show the formula)   CompanyCurre.pdf
Calculate the Current Ratios- (Please show the formula) CompanyCurre.pdf
 
Calculate the amount of money Sean had to deposit in an investment fun.pdf
Calculate the amount of money Sean had to deposit in an investment fun.pdfCalculate the amount of money Sean had to deposit in an investment fun.pdf
Calculate the amount of money Sean had to deposit in an investment fun.pdf
 
Calculate FST for the random-mating subpopulations below based on the.pdf
Calculate FST for the random-mating subpopulations below based on the.pdfCalculate FST for the random-mating subpopulations below based on the.pdf
Calculate FST for the random-mating subpopulations below based on the.pdf
 
CALCIUM PATHWAY trace a calcium ion from the small intestine to the in.pdf
CALCIUM PATHWAY trace a calcium ion from the small intestine to the in.pdfCALCIUM PATHWAY trace a calcium ion from the small intestine to the in.pdf
CALCIUM PATHWAY trace a calcium ion from the small intestine to the in.pdf
 
Cadux Candy Company's income statement for the year ended December 31-.pdf
Cadux Candy Company's income statement for the year ended December 31-.pdfCadux Candy Company's income statement for the year ended December 31-.pdf
Cadux Candy Company's income statement for the year ended December 31-.pdf
 
Calcium - Selonosis- characterized by hair loss- weile blotchy mail- g.pdf
Calcium - Selonosis- characterized by hair loss- weile blotchy mail- g.pdfCalcium - Selonosis- characterized by hair loss- weile blotchy mail- g.pdf
Calcium - Selonosis- characterized by hair loss- weile blotchy mail- g.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 

can someone fix the errors in this code- the name needs to be Fraction.pdf

  • 1. can someone fix the errors in this code? the name needs to be FractionDemo.java //author: Jazmine Tapia //date: 20 February 2023 //file: FractionDemo.java /* Lab 13 will also be using demonstation the the multiplication of two factors. */ //import statements for Scanner class import java.util.Scanner; // Define a public class called Fraction public class FractionDemo { // Define private instance variables for the numerator and denominator private int numerator; private int denominator; // Define a constructor for the Fraction class that takes a numerator and denominator as arguments public FractionDemo(int numerator, int denominator) { // Set the denominator using the setDenominator() method defined below setDenominator(denominator); // Set the numerator this.numerator = numerator; // Reduce the fraction reduce(); // Adjust the signs of the numerator and denominator if necessary adjustSigns(); } // Define a getter method for the numerator public int getNumerator() { return numerator; } // Define a setter method for the numerator public void setNumerator(int numerator) {
  • 2. this.numerator = numerator; } // Define a getter method for the denominator public int getDenominator() { return denominator; } // Define a setter method for the denominator public void setDenominator(int denominator) { // Set the denominator using the setDenominator() method defined below setDenominator(denominator); // Reduce the fraction reduce(); // Adjust the signs of the numerator and denominator if necessary adjustSigns(); } // Define a private method to set the denominator private void setDenominator(int denominator) { // If the denominator is zero, throw an exception if (denominator == 0) { throw new IllegalArgumentException("Denominator cannot be zero"); } // Set the denominator this.denominator = denominator; } // Define a private method to calculate the greatest common divisor of two integers private int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } // Define a public method to reduce the fraction public void reduce() {
  • 3. // Calculate the greatest common divisor of the numerator and denominator int gcd = gcd(numerator, denominator); // Divide both the numerator and denominator by the greatest common divisor to reduce the fraction numerator /= gcd; denominator /= gcd; } // Define a private method to adjust the signs of the numerator and denominator if necessary private void adjustSigns() { // If the denominator is negative, multiply both the numerator and denominator by -1 to adjust the signs if (denominator < 0) { numerator *= -1; denominator *= -1; } } // Define a public method to add two fractions public FractionDemo add(FractionDemo other) { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Get the numerator and denominator for the first fraction System.out.print("Enter the numerator of the first fraction: "); int num1 = input.nextInt(); System.out.print("Enter the denominator of the first fraction: "); int den1 = input.nextInt(); // Get the numerator and denominator for the second fraction System.out.print("Enter the numerator of the second fraction: "); int num2 = input.nextInt(); System.out.print("Enter the denominator of the second fraction: "); int den2 = input.nextInt(); // Create the two Fraction objects Fraction f1 = new Fraction(num1, den1); Fraction f2 = new Fraction(num2, den2); // Display the fractions System.out.println("f1==" + f1); System.out.println("f2==" + f2);
  • 4. // Perform arithmetic operations and display the results System.out.println(f1 + " + " + f2 + " = " + f1.add(f2)); System.out.println(f1 + " - " + f2 + " = " + f1.subtract(f2)); System.out.println(f1 + " * " + f2 + " = " + f1.multiply(f2)); System.out.println(f1 + " / " + f2 + " = " + f1.divide(f2)); } FractionDemo.java:110: error: illegal start of expression public static void main(String[] args) ^ FractionDemo.java:139: error: reached end of file while parsing } ^ 2 errors The code has to be a combination of these two codes //import statements for Scanner class import java.util.Scanner; public class FractionDemo { public static void main(String[] args) { // Variables for two fractions int num1, num2, den1, den2; // Create Scanner object to read keyboard input Scanner keyboard = new Scanner(System.in); // Get integers for f1 numerator and denominator System.out.print("Enter the numerator of the first fraction: "); num1 = keyboard.nextInt(); System.out.print("Enter the denominator of the first fraction: "); den1 = keyboard.nextInt(); // Get integers for f2 numerator and denominator System.out.print("Enter the numerator of the second fraction: "); num2 = keyboard.nextInt(); System.out.print("Enter the denominator of the second fraction: "); den2 = keyboard.nextInt(); // Instantiate the two fractions using the input values Fraction f1 = new Fraction(num1, den1); Fraction f2 = new Fraction(num2, den2);
  • 5. // Display the two fractions and their product System.out.println("f1 == " + f1); System.out.println("f2 == " + f2); System.out.println(f1 + " * " + f2 + " == " + f1.multiply(f2)); } } // Define a class for fractions class Fraction { private int numerator; private int denominator; // Constructor for Fraction class public Fraction(int num, int den) { this.numerator = num; this.denominator = den; // Check if denominator is zero and set it to 1 if true if (den == 0) { System.out.println("For Fraction: ( " + num + " / " + den + " ):"); System.out.println("Denominator cannot be zero; denominator value is being reset to one."); this.denominator = 1; // Reset denominator to 1 System.out.println("The Fraction is now: " + this); } } // Define a method for multiplying two fractions public Fraction multiply(Fraction other) { int num = this.numerator * other.numerator; int den = this.denominator * other.denominator; return new Fraction(num, den); } // Override the toString() method to display the Fraction objects as strings public String toString() { if (denominator == 1) return " ( " + numerator + " ) "; else return " ( " + numerator + " / " + denominator + " ) "; } }