Questions
I have a python program that must check if an integer is the sum of the...

I have a python program that must check if an integer is the sum of the squares of four consecutive prime numbers. However, when I run the program, it goes into an infinite while loop, and it doesn't give me any answer. I can only use the while, if, else, True and False commands. The code I'm using is

n1=2

  n2=3

  n3=5

  n4=7

  n = int(input("n: "))

  if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):

    print(n1,n2,n3,n4)

  else :

    i = 2

    pr = True

    next = n4 + 2

    while next <= n:

      while i < next and pr == True:

        if next%i == 0:

          pr = False

        else :

          pr = True

        i=i+1

      if pr == True:

        n1 = n2

        n2 = n3

        n3 = n4

        n4 = next

        if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):

          print(n1,n2,n3,n4)

        else:

          next = next + 2

    if (next>n):

      print("false")

And it only works with the number 87, since it's outside the while loop

In: Computer Science

................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...

................................................

................................................

This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference.

…………………………...……..

…………………………...…….

Instructions

LAB5 Instructions

Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width.

Your submission to BB should be your “Rectangle.java” and “CompareUsingequalsMethod.java” files only, not included in a .zip file or a .gpj file.

..............................................................

.............................................................

QC5

Circle.java

/**circle class*/
public class Circle{
//fields
private int radius;
//constructor
public Circle(int radius){
this.radius = radius;
  
}
//methods
public int getRadius() {
return radius;
}
public boolean equals(Circle inC1, Circle inC2){
if(inC1.getRadius() == inC2.getRadius())
return true;
else
return false;
  
}
}

…………………………………………….

…………………………………………………………..

CompareUsingequalsMethod.java

/**Demos using the equals method*/
public class CompareUsingequalsMethod{
public static void main(String[] args){
Circle circle1 = new Circle(3);
Circle circle2 = new Circle(3);
Circle circle3 = new Circle(5);
if (circle1.equals(circle1,circle2)) {
System.out.println(" Circle1 with radius " + circle1.getRadius() +
" is equal to circle2 with radius " + circle2.getRadius());

}
else{
System.out.println(" circle1 with radius " + circle1.getRadius() + " Radius is not equal to circle2 with radius " + circle2.getRadius());
  
}
if(circle3.equals(circle3,circle2)) {
System.out.println(" Circle3 with radius" + circle3.getRadius() +
" is equal to circle2 with radius" + circle2.getRadius());
}
else{
System.out.println(" circle3 with radius " + circle3.getRadius() + " Radius is not equal to circle2 with radius " + circle2.getRadius());
  

}
}
}

In: Computer Science

I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination...

I am stuck on this Java problem:

Create an Animal class with:

  • Attributes
    • Age
    • Rabies Vaccination Status
    • Name
    • Owner Name
  • A constructor to set values
  • Getters and Setters for all private attributes
  • A toString method that gives the data of all attributes

Create a Dog Class

  • Attributes
    • Distemper Vaccination Status
  • A constructor to set values in sub class
  • A constructor to set values in sub and super class
  • Getters and Setters for private attributes
  • A toString method that overrides the super class and adds Distemper Vaccination Status
  • A speak method that outputs "bark".

Create a Cat Class

  • Attributes
    • Feline Leukemia Vaccination Status
    • Declawed Status
  • A constructor to set values in sub class
  • A constructor to set values in sub and super class
  • Getters and Setters for private attributes
  • A toString method that overrides the super class and adds Feline Leukemia Vaccination Status
  • A speak method that outputs "Meow".

In: Computer Science

Lab 2 ∑={0,1} L = {w : if |w| is odd w begins with 11}. List...

Lab 2

∑={0,1} L = {w : if |w| is odd w begins with 11}.

List the indistinguishability classes for L(M). Include the following information for each class c, where wc is a string in c:

• A short description or characteristic function that describes the strings in c.

• Whether wc is or is not in L(M).

• ∀z(wcz ∈ L(M) iff … ). This rule must be different for each equivalence class — that is what makes them different classes.

• The class transitions for each symbol in Σ: wca ∈ [na] and Σ: wcb ∈ [nb], where na and nb are the numbers of equivalence classes.

In: Computer Science

Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...

Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a response variable. Send the name and response to your response dictionary as key-value pair. Once all inputs are in the program, print each poll participant's name and desired vacation destination. Use for loop to run through all key-pairs in the dictionary. Pay attention to your output. Print the dictionary to show the key-value pairs. Test program with 3 - 5 key value pairs.

In: Computer Science

Use Java GUI to write a chessboard 8 Queens Problem Write a program that can place...

Use Java GUI to write a chessboard

8 Queens Problem

Write a program that can place 8 queens in such a manner on an 8 x 8 chessboard that no queens attack each other by being in the same row, column or diagonal.

In: Computer Science

Please use java language in an easy way with comments! Expand your below program, so it...

Please use java language in an easy way with comments!

Expand your below program, so it can include cursed items (weapons that break, armor that causes more damage when worn). In order to do that, write an interface called "CursedItem". Then create subclasses of the armor, and weapon class that implement the CursedItem interface :

CursedWeapon: using a random number generator, there is a 4 in 10 chance that the weapon breaks during combat.

CursedArmor: this item amplifies the damage taken in battle instead of reducing it.

The interface only needs to have one method. In your driver class, create an example object for each of the new classes.

-----------------------------------------------------------------------------------------------------------

//Java code

public class GameItem {
    protected String itemName; //Name of the item

    //constructor

    public GameItem(String itemName) {
        this.itemName = itemName;
    }

    //use()
    public void use();
}

//======================================

/**
 * Weapon class that extends the GameItem class
 */
public class Weapon extends GameItem {
  
    private int damage;

    //constructor

    public Weapon(String itemName, int damage) {
        super(itemName);
        this.damage = damage;
    }

    public void use() {
        System.out.println("You now wield "+itemName+". This weapon does "+damage+" points of damage with each hit.");
    }
}

//==========================================

/**
 *  Armor class that extends the GameItem class
 */
public class Armor extends GameItem{
 
    private double protection;

    public Armor(String itemName, double protection) {
        super(itemName);
        this.protection = protection;
    }

    @Override
    public void use() {
        System.out.println("You have equipped "+itemName+". This item reduces the damage you take in combat by "+protection+" percent.");
    }
}

//=====================================

import java.util.ArrayList;

public class GameItemTest {
    public static void main(String[] args)
    {
        ArrayList<GameItem> inventory = new ArrayList<>();
        inventory.add(new Weapon("Sword",50));
        inventory.add(new Armor("Shield",85.9));

        for (GameItem g:inventory ) {
            g.use();
        }
    }
}

In: Computer Science

Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...

Solving Problems Using Recursion (Python):

To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables.

1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list.

Ex.

Input = [5555537777721]

Output = [53721]

In: Computer Science

determine the impulse response and the step response of the following causal systems plot the pole-zero...

determine the impulse response and the step response of the following causal systems plot the pole-zero patterns and determine which are stable

y(n) = 3/4y(n - 1)- 1/8y(n - 2) + x(n)

In: Computer Science

Write a tester program to test the mobile class defined below. Create the class named with...

Write a tester program to test the mobile class defined below.

Create the class named with your id (for example: Id12345678) with the main method.

  1. Create two mobiles M1 and M2 using the first constructor.
  2. Print the characteristics of M1 and M2.
  3. Create one mobile M3 using the second constructor.
  4. Change the id and brand of the mobile M3. Print the id of M3.

Your answer should include a screenshot of the output. Otherwise, you will be marked zero for this question.

public class Mobile {

private int id;

private String brand;

public Mobile() {

    id = 0;

    brand = "";

}

public Mobile(int n, String name) {

    id = n;

    brand = name;

}

public void setBrand(String w) {

    brand = w;

}

public void setId(int w) {

    id = w;

}

Public int getId() {

In: Computer Science

For this part of the assignment I want you to execute the script CS50_hw_session04b. This will...

For this part of the assignment I want you to execute the script CS50_hw_session04b. This will create a directory called hw_session04 in your home directory. Change directories to hw_session04.

- Execute file_1 in hw_session04

- Read file_2 in hw_session04

- Create the file dir_3/name with the output of the whoami command and the first secret code when you execute file_1. Save the output of executing file_1 exactly as shown. There should be two lines in dir_3/name.

- Create the file dir_3/now with the output of the date command and the second secret code when you read file_2. Save the output of reading file_2 exactly as shown. There should be two lines in dir_3/now.
- Create a file called jamesbond in hw_session04 with the output of the date command. Change the permissions of the file jamesbond so that user has no access, group has no access but other has full access.

- Create a file called myfriend in hw_session04 with the output of the date command. Change the permissions of the file myfriend so that user has full access, group has read and execute access and other has no access.

- Execute the following commands and add the output to a file call session04b_hw

--- Echo your first and last name as the first line of the file session04b_hw
--- Get a long listing of the files in hw_session04
--- Display the contents of the file dir_3/name
--- Display the contents of the file dir_3/now
--- Echo the following message "The permissions of jamesbond is XXX". When you echo this message replace the XXX with the numeric permission value for the file jamesbond before echoing out the message.

Once you have executed the above commands, look over the file session04b_hw and check that it looks like you would expect based on the commands you have executed.

In: Computer Science

Java program by using array only. II. The NumberTile Game (Unchanged) Number tiles are squares with...

Java program by using array only.

II. The NumberTile Game (Unchanged)

  • Number tiles are squares with a number from 1 to 9 at each side. A “board” for the game is a sequence of number tiles satisfying this property:

      The right side of each tile (except the last) = the left side of the next tile on the board

  • There are 2 players in the game, both of which are the computer. Each player starts with a "hand" of 5 number tiles. A move is made by a player removing tile from the hand and placing it on the board, if possible.   A tile may be placed on the board in any of 3 ways:
  1. As the new first tile
  2. Between two adjacent tiles
  3. As the new last tile
  • A number tile in the players hand can be rotated 90 degrees but once placed on the board its position is fixed
  • If none of the tiles in the hand can be placed on the board, then one random tile is added to the player's hand
  • A round is when player1 makes a move and then player2 makes a move (to make the game fair)
  • The game ends when all tiles are removed from one (or both) player's hand(s). If one player's hand is empty and the other player's hand still contains tiles, then the player with the empty hand is the winner. If both players’ hands are empty, then the game ends in a tie

III. Specifications

  • NEW: The output from 3 games must be written to an output file
  • Your program will consist of the same 5 classes
  • Use the skeletons of the NumberTile, Hand, Board, and TileGame classes provided
  1. Not allow to add or remove or relocate any methods
  2. Not allow to modify any of the method or instance variable declarations or add any new instance variables
  • create two Hand objects
  1. create a TileGame object using the two Hand objects
  2. call the toString() method of the Hand class for each initial hand and print them, properly labeled
  3. call the play() method of the TileGame class
  4. call the getResults() method of the TileGame class and print the string returned
  1. After each game, give the user the option to see another
  2. You must use the counter provided in each class as the upper bound on all loops that traverse the array! (see PartiallyFilled.java, online)
  3. You may use Arrays.copyOf, other Arrays class methods, and System.arraycopy ONLY to resize an array if it becomes full at it’s declared capacity. (If that happens, something’s wrong).
  4. No credit for using Arrays.copyOf or System.arraycopy to place a tile on the Board or remove or insert a tile from/to a Hand

NumberTile.java

import java.util.ArrayList ;
import java.util.Random;

// A NumberTile is a square tile with a number from 1 to 9 on each side
public class NumberTile 
{
    private ArrayList tile ;    // the 4-sided tile
    private static final Random gen = new Random() ;
      
    // Create a NumberTile object with 4 random ints in the range 1 to 9
    public NumberTile() 
    {
       // TO DO: Code the body of this method
    }
    
    // Rotate this NumberTile 90 degrees
    public void rotate() 
    {
       // TO DO: Code the body of this method
    }
    
    // Return the number on the left side of this NumberTile
    public int getLeft()
    {
        // Do NOT modify this method
        return tile.get(0) ;
    }
    
    // Return the number on the right side of this NumberTile
    public int getRight() 
    {
        // Do NOT modify this method
        return tile.get(2) ;
    }
    
    // Return this NumberTile as a multiline string in the form:
    //     9
    //  3     7
    //     6
    //
    public String toString() 
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return null ;
    }    
} // end of NumberTile class

Hand.java

import java.util.ArrayList;

// A player's hand of NumberTiles
public class Hand
{
    private ArrayList hand ;
    private final static int INITIAL_SIZE = 5 ;  // starting hand size
    
    // Creates a new hand of INITIAL_SIZE NumberTiles 
    public Hand()
    {
       // TO DO: Code the body of this method
    }
       
    // Get the NumberTile at the specified index in this Hand
    public NumberTile get(int index)
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return null ;
    }
    
    // Get the number of tiles in this Hand
    public int getSize()
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return -999 ;
    }
    
    // Add a new NumberTile to this Hand
    public void addTile()
    {
       // TO DO: Code the body of this method
    }
    
    // Remove the NumberTile at the specified index from this Hand
    public void removeTile(int index)
    {
        // TO DO: Code the body of this method
    }
    
    // Is this hand empty?
    public boolean isEmpty()
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return false ;
    }
    
    // Return this Hand as a multiline String.
    // If this Hand is empty, return an appropriate message
    public String toString()
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return "The hand" ;
    }
}
Board.java

import java.util.ArrayList;

// The board for the NumberTile game  
public class Board
{
    private ArrayList board ;    // the board for a NumberTile game
    
    // Creates a new Board containing a single NumberTile
    public Board()
    {
       // TO DO: Code the body of this method       
    }
    
    // Return the NumberTile at the specified index on this Board
    public NumberTile getTile (int index)
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return null ;   
    }
    
    // Return the current number of tiles on this Board
    public int getSize()
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return -999 ;
    }
    
    // Insert a new tile into this Board at the specified index
    public void addTile(int index, NumberTile tile)
    {
       // TO DO: Code the body of this method
    }
    
    // Return a multiline string containing all the tiles on this Board
    public String toString()
    {
       // TO DO: Code the body of this method
       
       // temporary return statement so program skeleton will compile and run
       return "The board" ;
    }           
}

TileGame.java

// Implements a domino-like game where two players, both of whom are
// the computer, take turns inserting NumberTiles into a Board
public class TileGame
{
   // instance vars
   private Board board ;     // the game board
   private Hand hand1 ;      // Player 1 hand
   private Hand hand2 ;      // Player 2 hand
   private String winner ;   // the winner - player1, player2,
                             // or a tie game
   
   // Creates a new TileGame with two initial hands and a board
   public TileGame(Hand firstHand, Hand secondHand)
   {
      // TO DO: Code the body of this method
   }
   
   // Players take turn moving until one or both hand are empty
   public void play()
   {
      // TO DO: Code the body of this method
   }

   // Utility method called by method makeMove.  Returns the index at which a
   // new tile will be inserted into the board, or -1 if the tile cannot
   // be inserted.  The new tile may be inserted either (1) between two 
   // existing tiles, (2) as the new first tile, or (3) as the new last tile
   private int getIndexForFit(NumberTile tile)
   {
      // TO DO: Code the body of this method
       
      // temporary return statement so program skeleton will compile and run
      return -1 ;
   }

   // Utility method called by method play().  Checks consecutive tiles in the 
   // hand - by calling method getIndexForFit() - to see if one can be inserted 
   // into the board. When the first tile that fits is found, removes it from
   // the hand, inserts it into the board, and the move ends.  The tile may be
   // rotated up to 3 times. If none of the tiles fit, adds a new, random tile
   // to the hand  
   private void makeMove(Hand hand)
   {
      // TO DO: Code the body of this method
   }
   
   // Return results of the game as a humongous multi-line String containing
   // the final board, both both player's final hands, and the winner
   public String getResults()
   {
      // TO DO: Code the body of this method
      // HINT: call toString for the board and for each hand and don't
      //       forget the winner
       
      // temporary return statement so program skeleton will compile and run
      return "Results" ;
   }
} // end of TileGame2 class

TileGameTester.java

// A test class for the NumberTile Game
public class TileGameTester
{
   public static void main(String[] args)
   {
      // TO DO: Code the body of this method
   }
} 

Basically, we will have 5 tiles on our hands. If tiles contain a number of the left/right number on board. We can place it on board.

Board:

5

4 6

2

so that we need to have 4/6 on our hand. if so, we can rotate the number for that tile and put it out.

4 2

2 5 ==> 6 4

6 5

TO board:

2

6 4

5

5

4 6

2

Starting a new game...

***** Player 1 Initial Hand *****

    7

5   7

    6

    4

1   1

    9

    2

2   6

    2

    9

7   7

    1

    9

6   5

    3

***** Player 2 Initial Hand *****

    7

6   2

    1

    8

2   8

    3

    9

2   4

    2

    1

9   6

    3

    9

2   3

    1

***** The Initial Board *****

    5

3   4

    1

***** The Final Board *****

    8

8   3

    2

    5

3   4

    1

    1

4   9

    1

    7

9   1

    7

    6

1   7

    2

    6

7   5

    7

    3

5   6

    9

    2

6   2

    2

    2

  2   9

    4

In: Computer Science

Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...

Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.

Please provide a solution in Java

In: Computer Science

Decimal value data types such as float and double represent the decimal number as an approximation....

Decimal value data types such as float and double represent the decimal number as an approximation. In other words, float or double arithmetic do not give exact answer but near approximation to the answer. As an example, run the following program and check its result:
#include <iostream>
using namespace std;
int main()
{
float x= 0.1 * 7;
if (x == 0.7)
cout<< "TRUE. \n";
else
cout<< "FALSE. \n";
return 0;
}
In some situations, we need our programs to give exact solutions instead of near to exact answers. Some programming languages provide a special data type called Decimal that represents decimal numbers without the use of approximation. Write a program in C++ that implements the Decimal data type using a class called BigDecimal. This class will allow users to create decimal values and perform several operations on these values. The class should use two member variables:
- the integer part saved as a string
- the decimal part saved as a string
For example, the following instance BigDecimal d("6.45678"); will store "6" in the first variable and "45678" in the second variable
The class should have the following functionalities:
Member Function Description
BigDecimal() The default constructor. Creates the number 0.0
BigDecimal(string) A constructor that accepts a string representing the numeric value with the decimal point.
== operator Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return true otherwise it should return false.
!= operator Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return false otherwise it should return true.
++ operator (prefix) This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object (using this pointer) after it is incremented.
++ operator (postfix) This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object before it is incremented.
<< operator Displays the numeric value of BigDecimal to the standard output.
>> operator Reads a string value from the standard input and stores it in the BigDecimal object.
double toDouble() Converts the BigDecimal value to a double.
You can assume that all the values of BigDecimal are nonnegative. Here is an example of how BigDecimal can be used:
BigDecimal x("45.67");
BigDecimal y("2.5");
//Should print 2.5
cout << x++ << endl;
//Should print 4.5
cout << ++x << endl;
//Should print false
cout << x==y << endl;

In: Computer Science

How can user error be controlled? Is it unavoidable?

How can user error be controlled? Is it unavoidable?

In: Computer Science