Ask a question from expert

Ask now

Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football

26 Pages6874 Words98 Views
   

Added on  2019-09-21

About This Document

This article contains Java programs for Fraction class, String and Date array, One Dimensional Array and Fantasy Football. The Fraction class program creates a class called Fraction and provides methods for toString, setFraction, equals, add, subtract, multiple, divide and reduce. The String and Date array program creates an array of Strings and an array of MyDate classes. The One Dimensional Array program creates an array of integers and prints the array. The Fantasy Football program reads in the information of fantasy football scores and displays the scores in a table format.

Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football

   Added on 2019-09-21

BookmarkShareRelated Documents
Fraction ProblemCreate a class called Fraction. Provide a constructor that takes 2 integers. Provide methods fortoString setFractionequals add subtract multiple dividereduce - make sure you get the previous methods done before worrying about reduce.The following will be your starting point: package fractions;import java.util.*;public class Fraction{ private Scanner scan = new Scanner(System.in); private int num=1; private int denom=1; public Fraction() { } public Fraction(int n, int d) { // Fill in code (good to use setFraction) } public void setFraction(int n, int d) { //Fill in code ... don't forget to reduce } public Fraction add(Fraction op) { //Fill in code ... don't forget to reduce // Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d) } public Fraction subtract(Fraction op) { //Fill in code ... don't forget to reduce // Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d) }
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_1
public Fraction multiply(Fraction op) { //Fill in code ... don't forget to reduce // Algebra HINT: a/b * c/d = (a*c)/ (b*d) } public Fraction divide(Fraction op) { //Fill in code ... don't forget to reduce // Algebra HINT: a/b / c/d = (a*d)/ (b*c) } private void reduce() { // Pseudo code: // set smaller = minimum ( abs(num), abs(denom)); // Loop through the possible divisors: 2, 3, 4, ... smaller // For each possible divisor: // while (num and denom are evenly divisible by divisor) // { // num /= divisor; // denom /= divisor; // smaller /= divisor; // } } public boolean equals(Fraction f) { // Assuming all fractions are reduced // Fill in code } public String toString() { // Fill in code } public void readin(String label) { while (true) // Keep trying if bad input is received { System.out.print(label); String temp = scan.next(); temp = temp.trim(); // get rid of white space at the beginning andend int index = temp.indexOf('/'); if (index >= 0) { String numStr = temp.substring(0, index); String denomStr = temp.substring(index+1); int n = Integer.parseInt(numStr); int d = Integer.parseInt(denomStr); setFraction(n,d); return; } else
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_2
System.out.println("Input Fraction missing / "); }//Keep trying until you get it right } public static void main(String[] args) { Fraction f1= new Fraction(); Fraction f2= new Fraction(); Fraction f3=null; Scanner scan = new Scanner(System.in); while(true) { System.out.println(); // Add a blank line System.out.print("Enter operation: + - * / q (q ==> quit) : "); String input = scan.next(); if (input.charAt(0) == 'q') break; // All done f1.readin("Enter Fraction 1: "); f2.readin("Enter Fraction 2: "); System.out.println("f1 = " + f1); System.out.println("f2 = " + f2); if (f1.equals(f2)) System.out.println("f1 and f2 are equal"); else System.out.println("f1 and f2 are not equal"); switch (input.charAt(0)) { case '+': f3 = f1.add(f2); System.out.println("f1+f2=" + f3); break; case '-': f3 = f1.subtract(f2); System.out.println("f1-f2=" + f3); break; case '*': f3 = f1.multiply(f2); System.out.println("f1*f2="+f3); break; case '/': f3 = f1.divide(f2); System.out.println("f1/f2="+f3); break; default: System.out.println("Illegal command: " + input ); break; }
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_3
}// end of while loop System.out.println("Bye"); } // end of main }Please try to run the following Fraction calculations: (note the answers are filled with ______)Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 1/3Enter Fraction 2: 4/6f1 = 1/3f2 = 2/3f1 and f2 are not equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 5/25Enter Fraction 2: 10/50f1 = 1/5f2 = 1/5f1 and f2 are equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 7/30Enter Fraction 2: 11/20f1 = 7/30f2 = 11/20f1 and f2 are not equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : -Enter Fraction 1: 11/12Enter Fraction 2: 4/5f1 = 11/12f2 = 4/5f1 and f2 are not equalf1-f2=_________Enter operation: + - * / q (q ==> quit) : *Enter Fraction 1: 5/6Enter Fraction 2: 1/4f1 = 5/6f2 = 1/4f1 and f2 are not equalf1*f2=_________Enter operation: + - * / q (q ==> quit) : /
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_4
Enter Fraction 1: 6/7Enter Fraction 2: 1/12f1 = 6/7f2 = 1/12f1 and f2 are not equalf1/f2=_________Enter operation: + - * / q (q ==> quit) : qByestring_array Create a package named string_array and write a program that creates an array of Strings with the name of "firstNames". Fill the array with the names(in this order): "George", "Fred", "Sam", "Mary", "Sarah", "Bella", "Joy", "Rita", "Marta", "Sue", "Nancy" Print the values of the firstNames array backwards. In otherwords, your output should be:Nancy Sue Marta Rita Joy Bella Sarah Mary Sam Fred GeorgeRun the program and insert the results in the appropriate section of the JH5 worksheet. date_arrayCreate a package named date_array and you will need 2 classes. Create a class called MyDate. This class only needs the following things:Instance variables for month, day and yearConstructor: MyDate(String month, int day, int year) - the code stores the values in 3 instance variablesString toString() method - returns a String containing your 3 instance variables.
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_5
Create another class called DateArray that creates an array of "MyDate" with the name of "dateArr".The array should have 4 entries and the entries should be filled with MyDate classes representingthe dates: May 16, 1984 November 14, 1978 September 21, 1980 July 3, 1987 The DateArray class should print the values of the dateArr array backwards. Your MyDate classes can be printed using a toString() method in the MyDate class.Run the program and insert the results in the appropriate section of the JH5 worksheet. one_dimensional_array problem (15 points):Create a package named one_dimensional_array and write a class that completes the following "OneDimensionalArrays" class. You will complete the class by filling in code wherever you see the comment://******* FILL IN CODE *********import java.util.Scanner;public class OneDimensionalArrays { int[] createIntegers(int size_of_array) { //******* FILL IN CODE ********* // Your code will create an array of ints as large as specified in size_of_array // Fill the array in with the values: 0, 100, 200, 300, .... // Return the array that you just created } void printArray(int[] myArray) { //******* FILL IN CODE ********* // Print out your array with one number per line. Get the size of the // array from the "myArray" parameter (no hard coding the size) }
Java program for Fraction class, String and Date array, One Dimensional Array and Fantasy Football_6

End of preview

Want to access all the pages? Upload your documents or become a member.