Question

JAVA PROGRAMMING LANGUAGE

Make a class named fraction. And make a program that accepts inputs from a user and creates outputs from those inputs. User cNotes: Use Array, ArrayList, or Vector to store the inputs. Class fraction must not contain inputs and outputs. Every inputsSAMPLE INPUT & OUTPUT

3.25

1 1/4

9/4

0

Case #1:

3.25

13/4

3 1/4

Case #2:

1.25

5/4

1 1/4

Case #3:

2.25

9/4

2 1/4

0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Fraction{

private int num, denom;

public Fraction(double d) {
String s = String.valueOf(d);
int digitsDec = s.length() - 1 - s.indexOf('.');
int denom = 1;
for (int i = 0; i < digitsDec; i++) {
d *= 10;
denom *= 10;
}

int num = (int) Math.round(d);
this.num = num;
this.denom = denom;
}

public Fraction(int num, int denom) {
this.num = num;
this.denom = denom;
}

public String toString() {
return String.valueOf(num) + "/" + String.valueOf(denom);
}

public static void main(String[] args) {

double a = 3.25;
System.out.println(new Fraction(a));

int b = (int)Math.floor(a);
System.out.println(""+b);
}
}

//mix Faction

String MixedFraction(Fraction f) {
int a = f.getNumerator() / f.getDenominator();
int b = f.getNumerator() % f.getDenominator();
return a != 0 ? (a + " " + b + "/" + f.getDenominator()) : (b + "/" + f.getDenominator());
}

Fraction f = new Fraction(8, 3);
System.out.println(MixedFraction(f));

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING LANGUAGE SAMPLE INPUT & OUTPUT 3.25 1 1/4 9/4 0 Case #1: 3.25 13/4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • JAVA

    Write a Java class (program) which inputs a simple integer and outputs a list of values from the input variable down to 0 (not including 0). You should only show every other value in the output, starting with the input value. You must use a for loop.For example, if the input is 23, the output should be:The input value was 2323 21191715131197531Code given:import java.util.Scanner;/**  * This program counts down from the input number down to (but not including)   * 0, skipping...

  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...

    Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali owns a coconut plantation and a number of monkeys for bringing down coconuts. Every day, Ali would record the number of coconuts brought down by his monkeys. At the end of a certain time period, he would determine from the data recorded, the lowest and the highest number of coconuts as well as their number of occurrences. Write a program...

  • C++ programming language , I need help writing the code . Please explain if you can...

    C++ programming language , I need help writing the code . Please explain if you can . Exercise 10: Unit conversion (10 points) Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches...

  • Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9...

    Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9 Ex: If the input is 5 7 9 11 -1, the output is: 7 9 The maximum number of inputs for any test case will not exceed 5. If the input exceeds this maximum, output "Too many inputs". Hint:...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT