Questions
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...

JAVA Programming

(Convert decimals to fractions)

Write a program that prompts the user to enter a decimal number and displays the number in a fraction.

Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at

https://liveexample.pearsoncmg.com/test/Exercise13_19.txt

for your code.

Sample Run 1

Enter a decimal number: 3.25
The fraction number is 13/4

Sample Run 2

Enter a decimal number: -0.45452
The fraction number is -11363/25000

The output must be similar to the Sample Runs ( Enter a decimal number: etc.)


Class Name MUST be Exercise13_19

If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.

RATIONAL CLASS:

/* You have to use the following template to submit to Revel.
   Note: To test the code using the CheckExerciseTool, you will submit entire code.
   To submit your code to Revel, you must only submit the code enclosed between
     // BEGIN REVEL SUBMISSION

     // END REVEL SUBMISSION
*/
import java.util.Scanner;

// BEGIN REVEL SUBMISSION
public class Exercise13_19 {
  public static void main(String[] args) {
    // Write your code
  }
}
// END REVEL SUBMISSION

// Copy from the book
class Rational extends Number implements Comparable<Rational> {
  // Data fields for numerator and denominator
  private long numerator = 0;
  private long denominator = 1;

  /** Construct a rational with default properties */
  public Rational() {
    this(0, 1);
  }

  /** Construct a rational with specified numerator and denominator */
  public Rational(long numerator, long denominator) {
    long gcd = gcd(numerator, denominator);
    this.numerator = (denominator > 0 ? 1 : -1) * numerator / gcd;
    this.denominator = Math.abs(denominator) / gcd;
  }

  /** Find GCD of two numbers */
  private static long gcd(long n, long d) {
    long n1 = Math.abs(n);
    long n2 = Math.abs(d);
    int gcd = 1;
    
    for (int k = 1; k <= n1 && k <= n2; k++) {
      if (n1 % k == 0 && n2 % k == 0) 
        gcd = k;
    }

    return gcd;
  }

  /** Return numerator */
  public long getNumerator() {
    return numerator;
  }

  /** Return denominator */
  public long getDenominator() {
    return denominator;
  }

  /** Add a rational number to this rational */
  public Rational add(Rational secondRational) {
    long n = numerator * secondRational.getDenominator() +
      denominator * secondRational.getNumerator();
    long d = denominator * secondRational.getDenominator();
    return new Rational(n, d);
  }

  /** Subtract a rational number from this rational */
  public Rational subtract(Rational secondRational) {
    long n = numerator * secondRational.getDenominator()
      - denominator * secondRational.getNumerator();
    long d = denominator * secondRational.getDenominator();
    return new Rational(n, d);
  }

  /** Multiply a rational number to this rational */
  public Rational multiply(Rational secondRational) {
    long n = numerator * secondRational.getNumerator();
    long d = denominator * secondRational.getDenominator();
    return new Rational(n, d);
  }

  /** Divide a rational number from this rational */
  public Rational divide(Rational secondRational) {
    long n = numerator * secondRational.getDenominator();
    long d = denominator * secondRational.numerator;
    return new Rational(n, d);
  }

  @Override  
  public String toString() {
    if (denominator == 1)
      return numerator + "";
    else
      return numerator + "/" + denominator;
  }

  @Override // Override the equals method in the Object class 
  public boolean equals(Object other) {
    if ((this.subtract((Rational)(other))).getNumerator() == 0)
      return true;
    else
      return false;
  }

  @Override // Implement the abstract intValue method in Number 
  public int intValue() {
    return (int)doubleValue();
  }

  @Override // Implement the abstract floatValue method in Number 
  public float floatValue() {
    return (float)doubleValue();
  }

  @Override // Implement the doubleValue method in Number 
  public double doubleValue() {
    return numerator * 1.0 / denominator;
  }

  @Override // Implement the abstract longValue method in Number
  public long longValue() {
    return (long)doubleValue();
  }

  @Override // Implement the compareTo method in Comparable
  public int compareTo(Rational o) {
    if (this.subtract(o).getNumerator() > 0)
      return 1;
    else if (this.subtract(o).getNumerator() < 0)
      return -1;
    else
      return 0;
  }
}

In: Computer Science

Create an example schema representing Edgar Rice Burroughs published works starting with him as an author...

Create an example schema representing Edgar Rice Burroughs published works starting with him as an author and representing most of the metadata present in the following site/list of books.

http://www.gutenberg.org/ebooks/author/48 (Links to an external site.)

You need to include 5 books in your example schemas.

Create a JSON schema representing 5 of an author (Edgar Rice Burroughs) works.

In: Computer Science

JAVA Start with the SelectionSort class in the zip file attached to this item. Keep the...

JAVA

Start with the SelectionSort class in the zip file attached to this item. Keep the name SelectionSort, and add a main method to it.

  • Modify the selectionSort method to have two counters, one for the number of comparisons, and one for the number of data swaps. Each time two data elements are compared (regardless of whether the items are in the correct order—we're interested in that a comparison is being done at all), increment the comparison counter. Each time two data items are actually swapped, increment the data swap counter.
  • At the end of the selectionSort method, print the size of the sorted array, and the counters. (Be sure to identify which counter is which in your print message
  • In your main method,
    • Declare a final int, NUM_ELEMENTS. Initially set NUM_ELEMENTS to 10 to debug your program.
    • Declare and create three double arrays of NUM_ELEMENTS length, lo2Hi, hi2Lo, random.
    • Initialize the first array, lo2Hi, with values 1.0, 2.0, …, NUM_ELEMENTS
    • Initialize the second array, hi2Lo with values NUM_ELEMENTS, 24.0,…, 1.0
    • Initialize the third array, random, with random double values between 0.0 and less than 1.0
    • call the selectionSort method using each array. (Note: you might want to print the array elements themselves for debugging purposes when NUM_ELEMENTS is small, but you’ll not want to print them with larger values for NUM_ELEMENTS.)
  • Run your program three times with different values for NUM_ELEMENTS: 1000, 2000 and 4000.

In your submission write some text describing the relationship between the number of comparisons of the various values of NUM_ELEMENTS. For example, what do we find if we divide the number of comparisons for 2000 elements by the number of comparisons for 1000 elements? What do we find if we divide the number of comparisons for 4000 elements by the number of comparisons for 2000 elements?

SELECTION SORT FILE MUST USE THIS IN PROGRAM!!! PLEASE

public class SelectionSort {
/** The method for sorting the numbers */
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex = i;

for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}

// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}

In: Computer Science

Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...

  1. Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions.
  2. Name your output file “output.txt”.
  3. Ensure the program consists of a main function and at least one other function.
  4. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen:
  • output the text of the file to screen
  • output the number of words in the file
  • output the number of sentences in the file
  • output the first sentence in the file
  • output the last sentence in the file
  • output the length of the first sentence
  • output the length of the last sentence
  • output the first word in the file
  • output the first letter in the file
  • output the last word in the file
  • output the last letter in the file

The input file, “input.txt” is called parse Text.txt

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate — we can not consecrate — we can not hallow — this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation, under God, shall have a new birth of freedom — and that government of the people, by the people, for the people, shall not perish from the earth.

In: Computer Science

This is one lab question. I can not get this to work because of the templates,...

This is one lab question. I can not get this to work because of the templates, please help. It is extremely important the driver is not changed!!!!

Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0.Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative.

Every object of type myArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appropriate error message. Consider the following statements:
myArray list(5);                                   //Line 1
myArray myList(2, 13);                      //Line 2
myArray yourList(-5, 9);                     //Line 3

The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], ..., list[4]; the statement in Line 2 declares myList to be an array of 11 com- ponents, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8].

  1. Add an overloaded operator!=(…) and operator==(…) functions.
  2. Use the attached driver to test the program (lab6_Driver.cpp):
   
#include <iostream> 
#include "myArray.h"

using namespace std;

int main()
{ 
    myArray<int> list1(5);
    myArray<int> list2(5);

    int i;

    cout << "list1 : ";
    for (i = 0 ; i < 5; i++)
        cout << list1[i] <<" ";
    cout<< endl;

    cout << "Enter 5 integers: ";
    for (i = 0 ; i < 5; i++)
        cin >> list1[i];
    cout << endl;

    cout << "After filling list1: ";

    for (i = 0 ; i < 5; i++)
        cout << list1[i] <<" ";
    cout<< endl;

    list2 = list1;
    cout << "list2 : ";
    for (i = 0 ; i < 5; i++)
        cout << list2[i] <<" ";
    cout<< endl;

    cout << "Enter 3 elements: ";

    for (i = 0; i < 3; i++)
        cin >> list1[i];
    cout << endl;

    cout << "First three elements of list1: ";
    for (i = 0; i < 3; i++)
        cout << list1[i] << " ";
    cout << endl;

    myArray<int> list3(-2, 6);

    cout << "list3: ";
    for (i = -2 ; i < 6; i++)
        cout << list3[i] <<" ";
    cout<< endl;

    list3[-2] = 7;
    list3[4] = 8;
    list3[0] = 54;
    list3[2] = list3[4] + list3[-2];

    cout << "list3: ";
    for (i = -2 ; i < 6; i++)
        cout << list3[i] <<" ";
    cout<< endl;

        if (list1 == list2)
                cout << " list 1 is equal to list2 " << endl;
        else
                cout << " list 1 is not equal to list2" << endl;

        if (list1 != list2)
                cout << " list 1 is not equal to list2 " << endl;
        else
                cout << " list 1 is equal to list2" << endl;

    system("pause");
    return 0;
}

So I need to write a myArray class that will work with this driver, that also overloads the operators. Do not change the driver. It must use a template <T>.

In: Computer Science

Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...

Java Language

Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data.

Code:

class Node {

int value;

Node nextNode;

Node(int v, Node n)

{

value = v;

nextNode = n;

}

Node (int v)

{

this(v,null);

}

}

class LinkedList

{

Node head; //head = null;

LinkedList()

{

}

int length()

{

Node tempPtr;

int result = 0;

tempPtr = head;

while (tempPtr != null)

{

tempPtr = tempPtr.nextNode;

result = result + 1;

}

return(result);

}

void insertAt(int v, int position)

{

Node newNode = new Node(v,null);

Node tempPtr;

int tempPosition = 0;

if((head == null) || (position ==0))

{

newNode.nextNode = head;

head = newNode;

}

else {

tempPtr = head;

while((tempPtr.nextNode != null)&&(tempPosition < position -1))

{

tempPtr = tempPtr.nextNode;

tempPosition = tempPosition + 1;

}

if (tempPosition == (position - 1))

{

newNode.nextNode = tempPtr.nextNode;

tempPtr.nextNode = newNode;

}

}

}

public String toString()

{

Node tempPtr;

tempPtr = head;

String result = "";

while(tempPtr != null)

{

result = result + "[" + tempPtr.value + "| ]-->";

tempPtr = tempPtr.nextNode;

}

result = result + "null";

return result;

}

}

public class LinkedListDemoInsDel

{

public static void main(String[] args)

{

LinkedList aLinkedList = new LinkedList();

aLinkedList.insertAt(1,0);

aLinkedList.insertAt(9,1);

aLinkedList.insertAt(13,2);

aLinkedList.insertAt(8,1);

aLinkedList.insertAt(3,2);

System.out.println(aLinkedList);

System.out.println("Largo de lista: " + aLinkedList.length());

}

}

In: Computer Science

For this IP, you will create a very simple drawing app using Android Studio. The purpose...

For this IP, you will create a very simple drawing app using Android Studio. The purpose of this assignment is to give you more building blocks to use when programming apps. For full credit for this assignment, you should complete the following: Create a menu and display menu items on the app bar Detect when the user touches the screen and moves a finger Be able to change the color and width of a line Be able to save an image To turn in this assignment, upload screenshots of the working app (from the emulator) as a Word document. The screenshots need to show that the app works and that all of the parts listed work. At a minimum, students should upload 2 screenshots showing: A drawing Menu showing the different color choices and line widths.

In: Computer Science

A (7, 4) cyclic code is designed with a generator polynomial, g(D) = D3 + D...

A (7, 4) cyclic code is designed with a generator polynomial, g(D) = D3 + D + 1. a) (10 points) Determine the code word for the message, 1010. b) (10 points) Determine the code word for the message, 1100. c) (9+1= 10 points) Determine the error polynomial for the received word, 1110101. Is the received word correct?

In: Computer Science

For java. It's your turn to write a test suite! Let's start out simple. Create a...

For java.

It's your turn to write a test suite! Let's start out simple. Create a public class TestArraySum that provides a single void class method named test. test accepts a single parameter: an instance of ArraySum.

Each ArraySum provides a method sum that accepts an int[] and returns the sum of the values as an int, or 0 if the array is null. However, some ArraySum implementations are broken! Your job is to identify all of them correctly.

To do this you should use assert to test various inputs. Here's an example:

assert sum.sum(null) == 0;

Your function does not need to return a value. Instead, if the code is correct no assertion should fail, and if it is incorrect one should.

As you design test inputs, here are two conflicting objectives to keep in mind:

  • Less is more. The fewer inputs you need to identify all broken cases, the more readable your test suite will be.
  • Think defensively. At the same time, you want to anticipate all of the different mistakes that a programmer might make. You've probably made many of these yourself! Examples include forgetting to check for null, off-by-one errors, not handling special cases properly, etc.

In: Computer Science

Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...

Please answer with code for C language

Problem: Counting Numbers

Write a program that keeps taking integers until the user enters -100.

In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered.

Sample Input/Output 1:

Input the number:

0

2

3

-9

-6

-4

-100

Number of positive numbers: 2

Number of Negative numbers: 3

Number of Zero: 1

In: Computer Science

Java homework problem. This is my hotel reservation system. I'm trying to add a few things...

Java homework problem. This is my hotel reservation system. I'm trying to add a few things to it.

You will be changing your Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects.

  1. For this project you will be modifying the Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects.

    1. You will be create a Room object that will allow the user to set the type of room, if they want pets, and if they want Oceanview.

      1. OV is $50 more

      2. Pets $25 more

      3. King, Suite and Queen style rooms and you can set the prices

    2. You will have an array for username and password that hold 3 userNames and 3 passwords. These will be parallel arrays. I am allowed to enter username and password 3 times and then I get kicked out.

    3. Main

      1. The main method will keep track of information for 5 room reservations objects all for 1 night

        1. Be sure to use looping, somewhere in the main java file.

        2. Create a method that will catch the object and create it. Remember you can pass by reference or return the object back to the main.

        3. Create a method to handle printing out each of the objects to the screen and the total for each room. (You can set this total in the Room Class if you wish.)

        4. Finally Create a method that will show out the grand total.

Here is my original code to be modified:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class hotelreservation {
  
  
      public static double roomSelection(Scanner scan)
   {

       String roomSelection;
       System.out.print("Please enter the type of room desired for your stay (King/Queen/Suite/Two doubles): ");
       roomSelection = scan.nextLine();
    
       if(roomSelection.equalsIgnoreCase("Suite"))
           return 275;
       else if(roomSelection.equalsIgnoreCase("Queen"))
           return 150;
       else if(roomSelection.equalsIgnoreCase("King"))
           return 150;
       else
           return 125;
   }

   public static double roomOceanView(Scanner scan)
   {
       String response;
       System.out.print("Would you like an oceanview room (Yes/No) ? ");
       response = scan.nextLine();
    
       if(response.equalsIgnoreCase("Yes"))
           return 45;
       else
           return 0;
   }

   public static double roomPets(Scanner scan)
   {
       String response;
       System.out.print("Do you have any pets (Yes/No) ? ");
       response = scan.nextLine();
    
       if(response.equalsIgnoreCase("Yes"))
           return 50;
       else
           return 0;
   }

   public static void main(String[] args) {
     
       DecimalFormat decFormat = new DecimalFormat("0.00");
       Scanner scan = new Scanner(System.in);
       double roomReservation[] = new double[3];
       double subTotal = 0;
    
       for(int x=0;x        {
           System.out.println("Welcome to our Hotel Room Reservation Pricing System.");
           System.out.println("Please answer the following questions regarding your reservation of room #"+(x+1)+" : ");
           roomReservation[x] = roomPets(scan);
           roomReservation[x] += roomSelection(scan);
           roomReservation[x] += roomOceanView(scan);
       }
     
       for(int x=0;x        {
           System.out.println("Total cost for room #"+(x+1)+" : $"+decFormat.format(roomReservation[x]));
           subTotal += roomReservation[x];
       }
    
       System.out.println("Subtotal: $"+decFormat.format(subTotal));
       double tax = subTotal*0.05;
       System.out.println("Total Tax (5%): $"+decFormat.format(tax));
       System.out.println("Grand Total: $"+(decFormat.format(subTotal+tax)));
       scan.close();
   }

}

In: Computer Science

For Java Let's get more practice testing! Declare a public class TestRotateString with a single void...

For Java

Let's get more practice testing! Declare a public class TestRotateString with a single void static method named testRotateString. testRotateString receives an instance of RotateString as a parameter, which has a single method rotate. rotate takes a String and an int as arguments, and rotates the passed string by the passed amount. If the amount is positive, the rotation is to the right; if negative, to the left. If the String is null, rotate should return null.

Your testRotateString method should test the passed implementation using assert. Here's an example to get you started:\

assert rotate.String.rotate(null, 0) == null;

As you create your test suite, consider the various kinds of mistakes that you might make when implementing rotate. Maybe getting the direction wrong, or using the modulus improperly? There are lots of ways to mess this up! You need to catch all of them. Good luck!

In: Computer Science

True or False: It's easy to loop through an array using a for loop in C++...

True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array

True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector

In: Computer Science

*2-  Describe the role of the communication layer, the network-wide state-management layer, and the network-control application layer...

*2-  Describe the role of the communication layer, the network-wide state-management layer, and the network-control application layer in the SDN controller.

*3- Name the four different types of ICMP messages including type and code. What two types of ICMP messages are received at the sending host executing the Traceroute program?

*4- Why are different inter-AS and intra-AS protocols used in the internet? Explain your answer.

In: Computer Science

Step 1: Respond to the following: Planning and Executing Chapter 2 discusses the various project management...

Step 1: Respond to the following: Planning and Executing Chapter 2 discusses the various project management processes. In your initial discussion post, address the following:

 What does research suggest about the amount of time that should be spent on the initiating and planning processes?

 Do you think that the suggested amount of time is realistic? Why or why not?

 Why do you think spending more time on planning helps reduce time spent on executing?

Step 2: Read other students' posts and respond to at least three other students. Use any personal experience if appropriate to help support or debate other students' posts. If differences of opinion occur, students should debate the issues and provide examples to support opinions.

In: Computer Science