Questions
Create a Java method that does the following: 1) Ask the user for a dividend and...

Create a Java method that does the following:

1) Ask the user for a dividend and a divisor both of "int" type.

2) Computes the remainder of the division. The quotient (answer) must be of the "int" type.

Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative.

Here is the code that I have so far I can't seem to get it correct. Here is the feedback from the instructor: It appears you are computing the quotient here. This program is supposed to compute the remainder. This can be fixed easily with one line of code.

Which line of code needs to be corrected to determine the remainder?

import java.util.Scanner;

public class Main {

   //method to get the floor value

   static int floorDivide(int a, int b)

   {

   if(a % b != 0 && ((a < 0 && b > 0) || (a > 0 && b < 0)))

   {

   return (a / b - 1);

   }

   else

   {

   return (a / b);

   }

   }

public static void main(String args[]) {

int a,b, result;

Scanner sc=new Scanner(System.in);

System.out.println("Please Enter a Dividend: ");

a=sc.nextInt();

System.out.println("Please Enter a Divisor: ");

b=sc.nextInt();

result= floorDivide(a,b);

System.out.println("The Result is: "+result);

}

}

In: Computer Science

Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3....

Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been provided to allow you to insure that your LinkedList implementation is correct. b. Check your progress by noting the number of passing and failing tests. As you implement more of the class, more tests will pass, until all tests are passing. c. The provided tests are sufficient to show insure the LinkedList class is implemented correctly. d. You are free to add more tests as you feel are necessary; however, none of the existing tests should be modified or removed. here is the provided code class

class LinkedList<E> {

    private Node<E> head;

    // the constructor.


    public LinkedList(Node<E> head) {
        this.head = head;
    }

    /**
     * TODO Implement this method.
     * Add the given element, value, to the end of the list.
     * @param value The value to be added.
     */
    public void add(E value) {

    }

    /**
     * TODO Implement this method.
     * Add the given element, value, to the list at the given index.
     * After this operation is complete, get(index) will return value.
     * After this operation, all elements that had an index
     * greater than index (as determined by get()) will have their index increased by one.
     * This operation is only valid for 0 <= index <= size().
     * @param index The index at which to add value.
     * @param value The value to be added.
     * @throws IndexOutOfBoundsException when index is less than zero or greater than size.
     */
    public void add(int index, E value) {

    }

    /**
     * TODO Implement this method.
     * Determine if the LinkedList is empty or not.
     * @return true if this LinkedList has no items. (This is the same as the size equal to zero.)
     * false if the size is greater than zero.
     */
    public boolean isEmpty() {
        return false;
    }

    /**
     * TODO Implement this method.
     * Return the size (number of items) in this LinkedList.
     * @return the number of items in the list.
     */
    public int size() {
        return -1;
    }

    /**
     * TODO Implement this method.
     * Return the element of the list at the given index.
     * This operation is only valid for 0 <= index < size().
     * This operation does not modify the list.
     * @param index The index at which to retrieve an element.
     * @return The element of the list at the given index.
     * @throws IndexOutOfBoundsException when index is less than zero or greater than or equal to size.
     */
    public E get(int index) {
        return null;
    }

    /**
     * TODO Implement this method.
     * Remove and return the first element (element number zero) from the list.
     * This operation is only valid for non-empty (size() > 0) lists.
     * @return The removed element.
     * @throws IndexOutOfBoundsException When the list is empty.
     */
    public E remove() {
        return null;
    }

    /**
     * TODO Implement this method.
     * Remove and return the element with the given index from the list.
     * This operation is only valid for 0 <= index < size().
     * After this operation, all elements that had an index
     * greater than index (as determined by get()) will have their index reduced by one.
     * @param index The index to remove an element from.
     * @return The removed element.
     * @throws IndexOutOfBoundsException when removing from index less than zero or greater than or equal to size.
     */
    public E remove(int index) {
        return null;
    }
}

In: Computer Science

Convert the following MIPS instructions into machine code. Directly write down the answer without justifying steps...

  1. Convert the following MIPS instructions into machine code. Directly write down the answer without justifying steps will result 0 credit. All steps must be typed in Word.
    1. add $s0, $s1, $s2
    1. 2. lw $s0, 15($s1)

3. beq $s0, $s1, LABEL   #consider LABEL represents the 16-bit value 0x00FF

In: Computer Science

USING JAVA A method that parses the valid word file to a list of Strings. You...

USING JAVA

  • A method that parses the valid word file to a list of Strings. You may use a loop in the parsing method.
  • A nonrecursive method that prints the input String, makes the first call to the recursive anagramizer method, sends the result to the filter method, and prints the filtered result. You may use this code:
  • private void anagramize(String inString) {
  •         System.out.println("input string: " + inString);
  •         List < String > l = filter(anagramizeRecursive(inString.toLowerCase()));
  •         System.out.println("Anagrams: " + l);
  • }

In: Computer Science

Why is this java code reaching a deadlock and how to I fix it without serializing...

Why is this java code reaching a deadlock and how to I fix it without serializing the code (i.e. I don't want to do not have the Girl authenticate the Boy then have the Boy authenticate the Girl)?


import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// Attempt at a simple handshake.  Girl pings Boy, gets confirmation.
// Then Boy pings girl, get confirmation.
class Monitor {
   String name;
        
   public Monitor (String name) { this.name = name; }
                   
   public String getName() {  return this.name; }
                                
   // Girl thread invokes ping, asks Boy to confirm.  But Boy invokes ping,
   // and asks Girl to confirm.  Neither Boy nor Girl can give time to their
   // confirm call because they are stuck in ping.  Hence the handshake 
   // cannot be completed.
   public synchronized void ping (Monitor p) {
      System.out.println(this.name + " (ping): pinging " + p.getName());
      p.confirm(this);
      System.out.println(this.name + " (ping): got confirmation");
   }
                                
   public synchronized void confirm (Monitor p) {
      System.out.println(this.name+" (confirm): confirm to "+p.getName());
   }
}

class Runner extends Thread {
   Monitor m1, m2;
                
   public Runner (Monitor m1, Monitor m2) { 
      this.m1 = m1; 
      this.m2 = m2; 
   }
                                                        
   public void run () {  m1.ping(m2);  }
}
                                                                        
public class DeadLock {
   public static void main (String args[]) {
      int i=1;
      System.out.println("Starting..."+(i++));
      Monitor a = new Monitor("Girl");
      Monitor b = new Monitor("Boy");
      (new Runner(a, b)).start();
      (new Runner(b, a)).start();
   }
}

In: Computer Science

Q 1 - The surface area of a square or rectangle can be determined by multiplying...

Q 1 - The surface area of a square or rectangle can be determined by multiplying the length by the breadth. In a method called GetArea(), write a Java program to determine the surface area of a given square or rectangle. Assume the measurements will be in metres. The program should prompt the user for length and breadth values. [15]

Input
None
Output
Given a rectangle with length 20 metres and breadth 15 metres, the output would be: 300 square metres

In: Computer Science

Task: there are mainly five types of system calls: Process Control, File Management, Device Management, Information...

Task: there are mainly five types of system calls: Process Control, File Management, Device
Management, Information Maintenance and Communication. Please write two python programs
to implement the above two types of system calls.

In: Computer Science

Your company is creating a new sales system. These are some of the concerns: Ease of...

Your company is creating a new sales system. These are some of the concerns:

  • Ease of data entry
  • Accuracy of data entered, stored, and managed
  • Ability to create reports
  • Cost to build and maintain the system

Role of Chief Information Officer (CIO) is assigned to you: Chief Information Officer (CIO): All IT is overseen by the CIO. The CIO is over the hardware, software, IT processes, management, support and security and privacy of all systems within the organization

To prepare for this Discussion, gather information from the library and research other web resources. Write a plan describing your role in developing the system, your tasks based on your role, and your input to a plan for developing the system.

The initial response should be 400–500 words with APA references.

In: Computer Science

I have been working on this assignment in Java programming and can not get it to...

I have been working on this assignment in Java programming and can not get it to work.

This method attempts to DECODES an ENCODED string without the key.
  
public static void breakCodeCipher(String plainText){
char input[]plainText.toCharArray();
for(int j=0; j<25; j++){
for(int i=0; i<input.length; i++)
if(input[i]>='a' && input[i]<='Z')
input[i]=(char)('a'+((input[i]-'a')+25)%26);
else if(input[i]>='A'&& input[i]<='Z')
input[i]=(char)('A'+ ((input[i]-'A')+25)%26);
}
System.out.println(plainText)
  
}

In: Computer Science

Task 3. Falling distance When an object is falling because of gravity, the following formula can...

Task 3. Falling distance

When an object is falling because of gravity, the following formula can be used to determine

the distance the object falls in a specific time period:

d=(1/2)gt2

The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the

amount of time, in seconds, that the object has been falling.

3.1.     Create a method: FallingDistance

Parameters:           t, object’s falling time (in seconds). t may or may not be an integer value!

Return value:         the distance, in meters, that the object has fallen during that time interval

Calculations:          Use Math.Pow() to calculated the square in the formula

3.2.     Demonstrate the method by calling it from a loop that passes the values 1 through 20 as arguments, and displays each returned value.

** JAVA PROGRAM **

In: Computer Science

Display the shortest words in a sentence in JAVA Write a method that displays all the...

Display the shortest words in a sentence in JAVA

Write a method that displays all the shortest words in a given sentence. You are not allowed to use array

In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence.

You must design the algorithms for both the programmer-defined method and the main method.

In: Computer Science

Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...

Leave comments on code describing what does what

Objectives:

1. To introduce pointer variables and their relationship with arrays

2. To introduce the dereferencing operator

3. To introduce the concept of dynamic memory allocation

A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses.

The address of a variable is given by preceding the variable name with the C++ address operator (&). The & operator in front of the variable sum indicates that the address itself, and not the data stored in that location.

cout << &sum; // This outputs the address of the variable sum

To define a variable to be a pointer, we precede it with an asterisk (*). The asterisk in front of the variable indicates that ptr holds the address of a memory location.

int *ptr;

The int indicates that the memory location that ptr points to holds integer values. ptr is NOT an integer data type, but rather a pointer that holds the address of a location where an integer value is stored.

Explain the difference between the following two statements:

  

int sum; // ____________________________

int *sumPtr; // ___________________________

Using the symbols * and &:

  1. The & symbol is basically used on two occasions.

  • reference variable : The memory address of the parameter is sent to the function instead of the value at that address.

  • address of a variable

void swap (int &first, int &second) // The & indicates that the parameters

{ // first and second are being passed by reference.

int temp;

temp = first; // Since first is a reference variable,

// the compiler retrieves the value

// stored there and places it in temp.

first = second // New values are written directly into

second = temp; // the memory locations of first and second.

}

   

2) The * symbol is used on

  • define pointer variables:

  • the contents of the memory location

int *ptr;



Experiment 1

Step 1:

Complete this program by filling in the code (places in bold). Note: use only pointer variables when instructed to by the comments in bold. This program is to test your knowledge of pointer variables and the & and * symbols.

Step 2:

Run the program with the following data: 10 15. Record the output here .

// This program demonstrates the use of pointer variables

// It finds the area of a rectangle given length and width

// It prints the length and width in ascending order

#include <iostream>

using namespace std;

int main()

{

int length; // holds length

int width; // holds width

int area; // holds area

int *lengthPtr; // int pointer which will be set to point to length

int *widthPtr; // int pointer which will be set to point to width

  cout << "Please input the length of the rectangle" << endl;

cin >> length;

cout << "Please input the width of the rectangle" << endl;

cin >> width;

// Fill in code to make lengthPtr point to length (hold its address)

// Fill in code to make widthPtr point to width (hold its address)

area = // Fill in code to find the area by using only the pointer variables

cout << "The area is " << area << endl;

if (// Fill in the condition length > width by using only the pointer variables)

cout << "The length is greater than the width" << endl;

else if (// Fill in the condition of width > length by using only the pointer

// variables)

cout << "The width is greater than the length" << endl;

else

cout << "The width and length are the same" << endl;

return 0;

}




Experiment 2: Dynamic Memory

Step 1:

Complete the program by filling in the code. (Areas in bold) This problem requires that you study very carefully. The code has already written to prepare you to complete the program.

Step 2:

In inputting and outputting the name, you were asked NOT to use a bracketed subscript. Why is a bracketed subscript unnecessary? Would using name [pos] work for inputting the name? Why or why not? Would using name [pos] work for outputting the name? Why or why not?

Try them both and see.

// This program demonstrates the use of dynamic variables

#include <iostream>

using namespace std;

const int MAXNAME = 10;

int main()

{

int pos;

char * name;

int * one;

int * two;

int * three;

int result;

// Fill in code to allocate the integer variable one here

// Fill in code to allocate the integer variable two here

// Fill in code to allocate the integer variable three here

// Fill in code to allocate the character array pointed to by name

cout << "Enter your last name with exactly 10 characters." << endl;

cout << "If your name has < 10 characters, repeat last letter. " << endl

<< "Blanks at the end do not count." << endl;

for (pos = 0; pos < MAXNAME; pos++)

cin >> // Fill in code to read a character into the name array // WITHOUT USING a bracketed subscript

cout << "Hi ";

for (pos = 0; pos < MAXNAME; pos++)

cout << // Fill in code to a print a character from the name array // WITHOUT USING a bracketed subscript

cout << endl << "Enter three integer numbers separated by blanks" << endl;

// Fill in code to input three numbers and store them in the

// dynamic variables pointed to by pointers one, two, and three.

// You are working only with pointer variables

//echo print

cout << "The three numbers are " << endl;

// Fill in code to output those numbers

result = // Fill in code to calculate the sum of the three numbers

cout << "The sum of the three values is " << result << endl;

// Fill in code to deallocate one, two, three and name

return 0;

}

Sample Run:

Enter your last name with exactly 10 characters.

If your name < 10 characters, repeat last letter. Blanks do not count.

DeFinooooo

Hi DeFinooooo

Enter three integer numbers separated by blanks

5 6 7

The three numbers are 5 6 7

The sum of the three values is 18




Experiment 3: Dynamic Arrays

Question: Fill in the code as indicated by the comments in bold.

// This program demonstrates the use of dynamic arrays

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

float *monthSales; // a pointer used to point to an array

// holding monthly sales

float total = 0; // total of all sales

float average; // average of monthly sales

int numOfSales; // number of sales to be processed

int count; // loop counter

cout << fixed << showpoint << setprecision(2);

cout << "How many monthly sales will be processed? ";

cin >> numOfSales;

// Fill in the code to allocate memory for the array pointed to by

// monthSales.

if ( // Fill in the condition to determine if memory has been

// allocated (or eliminate this if construct if your instructor

// tells you it is not needed for your compiler)

)

{

cout << "Error allocating memory!\n";

return 1;

}

cout << "Enter the sales below\n";

for (count = 0; count < numOfSales; count++)

{

cout << "Sales for Month number "

<< // Fill in code to show the number of the month

<< ":";

// Fill in code to bring sales into an element of the array

}

for (count = 0; count < numOfSales; count++)

{

total = total + monthSales[count];

}

average = // Fill in code to find the average

cout << "Average Monthly sale is $" << average << endl;

// Fill in the code to deallocate memory assigned to the array.

return 0;

}

Sample Run:

How many monthly sales will be processed 3

Enter the sales below

Sales for Month number 1: 401.25

Sales for Month number 2: 352.89

Sales for Month number 3: 375.05

Average Monthly sale is $376.40

In: Computer Science

JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing...

JAVA!!!!

int[][] BingoCardArray = new int[5][5];

Use a nested for-loop, to populate the 2-D array representing 5 columns for B – I – N – G – O, where
row 1 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75

row 2 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 - 75

row 3 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75

row 4 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 - 75

row 5 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75


The playGame()method in the BingoGame (Driver) class will loop to generate 50 random numbers in the range of 1 to 75.  This simulates drawing 50 numbers in a BINGO game.  The method will pass the number to a method, checkBingo()in the domain class, BingoCard, which will check if the number received as a parameter is in the B range (1 – 15), I range (16 – 30), N range (31 – 45), G range (46 -60) or O range (61 – 75).  According to the range it is in, the method will check each of the cells in designated column (col 0 = B; col 1 = I; col 2 = N; col 3 = G; col 4 = O).  Example: randomNum 27 was randomly generated, which belongs to the I-range, column 1, checking rows 0 to 4.

for (int row = 0; row <= 4; row++)
{  
     //hard-code column 1 since checking I-column:
    if (randomNum == BingoCardArray[row][1])

    {
             BingoCardArray[row][1] = 0;
    }
}

etc.    //the 0 being moved to the number simulates putting a chip on the Bingo card, marking that the
          // number was called.

The gotBingo()method in the Bingo (Domain) class will return TRUE if 5 numbers were set to 0 either horizontally, vertically, or diagonally.  Otherwise, the method will return FALSE.

Each horizontal checkis a set of if-else if statements

for (int row = 0; row < 5; row++)
{
     int rowTotal = 0;

     for (int col = 0; col < 5; col++)
    {

           //add up all the column values in that row, and see if they add up to 0.

           ….

     }

}

Each vertical checkis a set of if-else if statements

for (int col = 0; col  < 5; col++)
{
     int colTotal = 0;

     for (int row = 0; row < 5; row++)
    {

           //add up all the row values in that column, and see if they add up to 0.

           ….

     }

}

//for diagonals check….
for (int row = 0; row < 5; row++)
{
     int diagonal1Total = 0;

     int diagonal2Total = 0;

     for (int col = 0; col < 5; col++)
    {

           //figure out if it is a diagonal going one way

           //figure out if it is a diagonal going the other way

           ….

     }

}

When gotBingo()ends, it will have either returned a TRUE or a FALSE.

In the driver class, the totalGamesWon is a global variable for keeping track of how many games the user wins instead of the computer. Before the

The determineWinner()method in the Bingo (Driver) class will call the gotBIngo() method in the domain class, and if it returns true, it will add 1 to the totalGamesWon, otherwise it will leave the totalGamesWon alone.

Finally, the mainmethod will contain a do-while loop that will execute at least once, and do the following:

  1. Instantiate the BingoCard class into an object
  2. Call the playGame() method in the driver class
  3. The playGame() method will call the checkBingo(int aNum) in the domain class
  4. Call the determineWinner() method in the driver class, which calls the gotBingo() method in the domain class, and add 1 to totalGamesWom whenever gotBingo() returns true.
  5. Ask user if he/she wants to repeat and play BINGO again.

In: Computer Science

Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...

Write in drjava

Problem

Design and implement these 4 files:

  1. A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes
  2. Tree inherits from Plant and adds a height attribute
  3. Flower inherits from Plant and adds a color attribute
  4. A driver file to test the 3 classes above.

The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set (mutator) methods for each attribute, and a toString method

Child classes should call parent methods whenever possible to minimize code duplication.

The driver program must test all the methods in each of the classes. Include comments in your output to describe what you are testing, for example   System.out.println(“testing Plant toString, accessor and mutator”);. Print out some blank lines in the output to make it easier to read and understand what is being output.

Assignment Submission:

Submit a print-out of each class file, the driver file and a sample of the output.

Marking Checklist

  1. Does EACH class have all the usual methods?
  2. Are all methods in EACH class tested, including child objects calling inherited parent methods?
  3. Does the child class call the parent’s constructor?
  4. Does the child class override the parent’s toString?
  5. Does the output produced have lots of comments explaining what is being output?
  6. Does each class, and the output, have blank lines and appropriate indenting to make them more readable?

In: Computer Science

1) Write Java application that asks the user to enter the cost of each apple and...

1) Write Java application that asks the user to enter the cost of each apple and number of apples bought. Application obtains the values from the user and prints the total cost of apples.
2) Write a java application that computes the cost of 135 apples, where the cost of each apple is $0.30.
3)Write a java application that prepares the Stationery List. Various entries in the table must be obtained from the user. Display the Stationary list in the tabular format

In: Computer Science