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 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:
Create a Dog Class
Create a Cat Class
In: Computer Science
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 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 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 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 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 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 your id (for example: Id12345678) with the main method.
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
In: Computer Science
Java program by using array only.
II. The NumberTile Game (Unchanged)
The right side of each tile (except the last) = the left side of the next tile on the board
III. Specifications
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 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. 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?
In: Computer Science