1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pshichka [43]
3 years ago
11

Change the Towers of Hanoi program so that it does the following: a)Counts the number of ring moves and prints that - instead of

the sequence of the moves. Use a static variable count of type int to hold the number of moves. b)Repeatedly prompts the user for the number of rings and reports the results, until the user enters a number less than 0
Computers and Technology
1 answer:
aniked [119]3 years ago
8 0

Answer:

Following are the program in the Java Programming Language.

//import scanner class package

import java.util.Scanner;

//define class

public class Tower_of_Hanoi {

//define static integer variable

public static int count = 0;

//define function

public static void Permute_Arrange(int n, char x, char ax, char to) {

//set if statement

if (n == 1) {

//increament in count by 1

++count;

}

//otherwise  

else  

{

Permute_Arrange(n - 1, x, to, ax);

++count;

Permute_Arrange(n - 1, ax, x, to);

}

}

//define main function

public static void main(String[] args)  

{

//set scanner type object

Scanner sc = new Scanner(System.in);

//print message

System.out.println("Enter less than 0 to exit");

//set the while infinite loop

while(true)

{

//print message

System.out.print("Enter the number of Disks: ");

//get input from the user

int num_of_disk = sc.nextInt();

//set the if statement to break the loop

if(num_of_disk<0)

{

//exit from the loop

System.exit(0);

}

//call the function

Permute_Arrange(num_of_disk, 'A', 'B', 'C');

//print message with output

System.out.println("Total number of Disc Moves is: " + count);

count = 0;

}

}

}

<u>Output:</u>

Enter less than 0 to exit

Enter the number of Disks: 4

Total number of Disc Moves is: 15

Enter the number of Disks: 7

Total number of Disc Moves is: 127

Enter the number of Disks: -1

Explanation:

Here, we define a class named "Tower_of_Hanoi"

  • Set the integer data type static variable "count" and initialize the value to 0.
  • Define void data type static function "Permute_Arrange" and pass three characters type arguments "x", "ax", and to and one integer type argument "n", inside it we set if condition to increment in the count variable otherwise call the function.
  • Finally, we define the main function to get input from the user and pass the argument list in function and call the function.
You might be interested in
​ In addition to analyzing logic and program code, a project team usually holds a session with users, called a _____, to review
andreev551 [17]

Answer:

design walk-through

Explanation:

In addition to analyzing logic and program code, a project team usually holds a session with users, called a <u>design walk-through</u>, to review the interface with a cross-section of people who will work with the new system and ensure that all the necessary features have been included.

A design walk-through is a quality practice that allows designers to obtain an early validation of design decisions related to the development and treatment of content, design of the graphical user interface, and the elements of product functionality.

8 0
3 years ago
ppose we have a Rectangle class that includes length and width attributes, of type int, both set by the constructor. Define an e
vichka [17]

Answer:

Check the explanation

Explanation:

#include <bits/stdc++.h>

using namespace std;

class Rectangle{

  public:

      int length;

      int breadth;

      Rectangle(int l,int b){

          length = l;

          breadth = b;

      }

      int area(){

          return length*breadth;

      }

      int perimeter(){

          return 2*(length+breadth);

      }

      bool equals(Rectangle* r){

          // They have the exact same length and width.

          if (r->length == length && r->breadth == breadth)

              return true;

          // They have the same area

          if (r->area() == area())

              return true;

          // They have the same perimeter

          if (r->perimeter() == perimeter())

              return true;

          // They have the same shape-that is, they are similar.

          if (r->length/length == r->breadth/breadth)

              return true;

          return false;

      }

};

int main(){

  Rectangle *r_1 = new Rectangle(6,3);

  Rectangle *r_2 = new Rectangle(3,6);

  cout << r_1->equals(r_2) << endl;

  return 0;

}

8 0
3 years ago
Create a Rational number class in Java using the same style as the Complex number class created in class.(The in class example c
Elis [28]

Answer:

Explanation:

/*

*    This program adds, subtracts, multiplies, and divides two rational numbers.

*/

public class Main {

  //Main method

   public static void main(String[] args) {

       Rational a = new Rational(3, 4);   // input for first rational number

       Rational b = new Rational(2 , 5);   // input for second rational number

       System.out.println(a + " + " + b + " = " + a.add(b));

       System.out.println(a + " - " + b + " = " + a.sub(b));

       System.out.println(a + " * " + b + " = " + a.mul(b));

       System.out.println(a + " / " + b + " = " + a.div(b));

   }

}

class Rational {

   public Rational(int num, int den) {

      numerator = num;

      denominator = den;

     

      //ensures a non-zero denominator

      if (den == 0)

          den =1;

     

      //stores a negative sign in numerator

      if (den < 0) {

          num = num * -1;

          den = den * -1;

      }

   }

   //return numerator

   public int getNumerator() {

     

      return numerator;

   }

 

   //return denominator

   public int getDenominator() {

     

      return denominator;

   }

 

   //return reciprocal method

   public Rational reciprocal() {

     

      return new Rational(denominator, numerator);

   }

   /*   Addition Class

    *        Find common denominator by multiplying the denominators

    *        Store number values (numerator / common denominator) as num1 and num2

    *        Sum both numbers

    */

   public Rational add(Rational r2) {

      int comDen = denominator * r2.getDenominator();

      int num1 = numerator * r2.getDenominator();

      int num2 = r2.getNumerator() * denominator;

      int sum = num1 + num2;

      return new Rational (sum, comDen);

   }

   //Subtraction Class - same implementation as Addition Class

   public Rational sub(Rational r2) {

   

     int comDen = denominator * r2.getDenominator();

     int num1 = numerator * r2.getDenominator();

     int num2 = r2.getNumerator() * denominator;

     int difference = num1 - num2;

     return new Rational (difference, comDen);

  }

   

  /*   Multiplication Class

   *        Multiply numerator with r2 numerator

   *        Multiply denominator with r2 denominator

   */

  public Rational mul(Rational r2) {

   

     int num = numerator * r2.getNumerator();

     int den = denominator * r2.getDenominator();

     return new Rational (num, den);

  }

  /*   Divide Class

   *        Multiply number by r2 reciprocal

   */

  public Rational div(Rational r2) {

      return mul (r2.reciprocal());

  }

  /*   toString Method

   *        Returns rational number as a string - in parenthesis if a fraction

   */

  public String toString() {

     

      return (((numerator == 0) ? "0" : ( (denominator == 1) ? numerator + "": "(" + numerator + "/" + denominator + ")")));

 

      /* Logic for toString method

      * if (num == 0) {

              return "0";

          } else {

              if (denominator == 1){

                  return numerator;

              } else {

                  return numerator + "/" + denominator;

              }

      */

   }

  // Declare variables

  private int numerator, denominator;

 

}

output

(3/4) + (2/5) = (23/20)                                                                                                                                      

(3/4) - (2/5) = (7/20)                                                                                                                                      

(3/4) * (2/5) = (6/20)                                                                                                                                      

(3/4) / (2/5) = (15/8)                                                                                                                                      

6 0
2 years ago
Select all the correct answers.
TEA [102]

Answer:

comlleting novice-level certifications working on one

4 0
2 years ago
Me Completan Pliiiis
Brut [27]
Can you translate please i’ll help better
4 0
3 years ago
Other questions:
  • What is the definition of legal intrusion
    14·1 answer
  • Which of the following describes a BYOD?
    15·1 answer
  • It is a function that ends the connection to the database.
    9·2 answers
  • Which protocol is used by the client for microsoft networks and file and printer sharing for microsoft networks to communicate w
    10·1 answer
  • Write a calculator program that keeps track of a subtotal like real calculators do. Start by asking the user for an initial numb
    12·1 answer
  • You are adding a new 500 GB hard drive to your computer. The hard drive must be formatted with a file system to organize files i
    8·1 answer
  • Write a program that uses a dictionary to store students birthdays. Your program should ask the user what their name is, and loo
    11·1 answer
  • Prokaryotes are __________________ and include ________________________.
    8·1 answer
  • ________ technologies are technologies that enable the incremental improvement of products and services.
    10·2 answers
  • Operating systems move code and data, as necessary, to a portion of the disk that is used as if it were memory, not just disk st
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!