Create Class Called Factorial Algorithm Which Will Compute / Print Factorial Of Integer Number On Screen

Dec 27, 2014

1)A factorial of a number X is equal to X*(X-1)*(X-2)*...*1.For example,3! is equal 3*2*1=6.Create a class called Factorial Algorithm which will compute and print the factorial of an integer number on the screen

2)Write a Java program to accept eight integers and a search element from the user and display whether the element is found or not.(Hint:use bubble sorting and binary search)

View Replies


ADVERTISEMENT

Reading Non-Negative Integer And Computing Its Factorial

Oct 3, 2014

I am attempting to write a program that reads a nonnegative integer and computes and prints its factorial. So far I have: Java Code: import java.util.Scanner;

public class Chapter3point37 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int nonNegative = 5;
int count=1;
int product=1;
int factor=1;
System.out.println("Input a nonnegative integer: ");
nonNegative = input.nextInt();

[code]...

how I should correctly prompt the user to input the values.

View Replies View Related

Calculating Factorial Of Int Number

Mar 2, 2014

I'm working on some exercises and I'm having some problems with a method. I want to create a method to calculate the Factorial of an int number. I already wrote code that asks the user to input an int number and it calculates the Factorial, and it works fine

i.e.: if I input 5 it outputs

5! = 120

as it should. Here's the code:

import java.util.Scanner;
public class Factorial1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int total = 1;
 
[Code] ....

Now I want to make a method to re-use this code in other programs and I wrote this program:

public class TestClass {
public static void main(String[] args) {
System.out.print(factorial(5));
}
  public static int factorial(int x) {
int total = 0;

[Code] ....

But when I run this program it outputs 0 instead of 120. What is wrong with this code as it compiles just fine but doesn't work as intended.

View Replies View Related

Doing Factorial Of Number Within Certain Constraints

Feb 5, 2014

I'm suppose to be doing a factorial of a number within certain constraints (say a number between 5 and 10). I get the factorial easy enough but I'm getting it for every number no matter what I put in.

textio is used instead of scanner for our class.

Java Code:

public class Factorial {
public static void main(String[] args) {
int max = 0;
int x = 0;
long y = 0;

[Code] .....

Ideally I should be able to put in a number outside the range and the program will do nothing until I put a number in with the correct value.

View Replies View Related

Simple For Loop - Determine Factorial Of A Number Entered By User

Jun 28, 2014

I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.

What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:

class factorial {
public static void main( String[] args) {
Scanner scan = new Scanner(System.in );
int num;
int product = 1;

[Code] ....

View Replies View Related

Factorial - Writing Iterative Program Recursively

Apr 30, 2015

It was quite recently that Data Structures was introduced to me, so I started out writing some iterative programs recursively.I found some strange output which shouldn't have come out but if you take a look at these three codes

long factorial(long n)
{
if(n == 1)
{
return 1;
}
else
{
result = n*factorial(n-1);

[Code] ....

These are three versions of the code, achieving the same objective of obtaining a given number and returning the factorial, but in spite of the changes made to the code, they produce the same result. I needed a reason as to why it is so? I tried to dry run all the codes but at some point or the other I got confused, and had to start all over again and couldn't come up with a proper result.

View Replies View Related

Create A Method Called MirrorImage Which Takes Two Integer Arrays As Input Parameters

Jan 8, 2009

Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.

Examples:

data1:{1,2,3}
data2:{3,2,1}
==> true

[code].....

I'm pointing a place outside of the array or something

runtime error (line 8, column 0): java.lang.ArrayIndexOutOfBoundsException: 10

-> array.ArrayQuestions.mirrorImage()

View Replies View Related

Using Nested Loops In A Class Called Print Patterns

Mar 7, 2014

Write a method to print each of the following patterns using nested loops in a class called PrintPatterns. The signature of the methods are:

public static void printPatternX(int size) //'X' from 'A' to....., size is a positive integer.

#
##
###
####
#####
######
#######
########

View Replies View Related

Create A Class Called Dice To Represent SINGLE Cube

Apr 15, 2014

I need to work on this "Dice" program. I've done it twice already. I've also been pouring over examples on here and elsewhere online, but none of them exactly match what I'm doing, as they all say "Pair of Dice".Mine says: "Create a class called Dice to represent a SINGLE cube". It should have a method called roll() that randomly selects a number from 1-6 for the value of the dice."

It has to use java.util.random, not math.java, and it has to have numberShowing:int, roll():int, and main() all in it.The last part reads "Create a test main method for the Dice class that creates a dice and rolls it many times. Can you keep track of how many times a number comes up? Describe how or implement it in the program." I have started at this computer for hours, and read as much info as I can.

View Replies View Related

5 Digit Integer - Print If Input Number Is Palindrome Or Not

Apr 21, 2015

I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?

import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int number;
int digit;
int temp;

[code]....

View Replies View Related

Write A Class Called Coin Which Will Be Used By A Program Called CountFlips

May 1, 2014

i am trying to write a coin program:Write a class called Coin which will be used by a program called CountFlips.

1. The class, is an object created from this program. It is composed of data values and methods. It should contain a main method and should output the number of flips done (an integer that will be read from the user), the number of heads and tails that occur.

2. The CountFlip class will flip a coin multiple times and counts the number of ‘heads’ and ‘tails’ that result. This class should have a method called flip() of type void; a method called isHead() of type Boolean and a toString() method that will return the current face of the coin as a string.

So i created 2 classes, one called Coin.java & the other CountFlips.java,

PHP Code: package test;
public class Coin {
private final int heads = 0;
private final int tails = 1;
private int facetype;
private int flips;

[code]....

View Replies View Related

Compute Recursively Total Number Of Blocks In Triangle With Given Number Of Rows

Jul 8, 2014

We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.

triangle(0) → 0

triangle(1) → 1

triangle(2) → 3

View Replies View Related

Create A Program Called MyStack

Apr 15, 2014

We have to create a program called MyStack.In Push: Add one element at the end of the buffer.In Pull: Remove one element from the end of the buffer. In isEmpty: Should check if the list is empty or not and return the result as a boolean variable.

I am totally confused, I wrote the buffer and the array, but how do i add an remove element from the buffer?How do i add an element? What Statement do i Use?

Java Code: public class MyStack<T> implements Stack<T> {
private final T[] ringbuffer;
int index = 0;
int last;

[code]...

View Replies View Related

Display Two Numbers / Integer Code And Computed Result To Screen

Oct 26, 2014

Write a program that will read two numbers and an integer code from the keyboard. The value of the integer code should 1, 2, 3, 4. If the value of the code is 1, compute the sum of the two numbers. If the code is 2, compute the difference (first number minus second). If the code is 3, compute the product of the two numbers. If the code is 4, and the second number is zero, compute the quotient (first divided by second). If the code is not equal to 1,2,3,4, display an error message. The program is then to display two numbers, the integer code and the computed result to the screen

here is the code that I have so far:

public static void main(String[] args) {
Scanner read = new Scanner (System.in);
int num1, num2, code, sum;
System.out.println("Please enter a number");
num1 = read.nextInt();

[Code] .....

View Replies View Related

Add Up 4 Numbers And Print Out On Screen

Jan 20, 2014

I just started taking a Java class and i'm having problems with this code,

I'm trying to add up the 4 numbers have them print out on screen.

public class finalGrade
{
public static void main(String[] arg)
{
double pLp = 28.3; //Participation, lab work, and programming

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Print Preview On Screen For JTable

Apr 9, 2014

I want to build a class that takes a JTable and previews on screen how it will be printed on the default printer. This is what we use to call as "Print Preview", just like MS Word, Excel, etc. As we already know Java does not support this functionality out-of-the-box.

Based on some examples I've found googling I built two classes. The first one TestPreview.java justs shows the JTable and the second one PrintPreview.java previews how the JTable will be printed on the printer.

I have two buttons to go through the pages, "PREV" and "NEXT" and "Print' button to print the JTable. My problem is that when my JTable has too many rows and spans in more than one page PrintPreview does not work. It shows the same page again and again. Also, it does not show the entire JTable data, but only some of them and I can't figure out why.

Below is my two classes.

TestPreview.java
package printpreviewdemo;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

[Code] ....

Below are my screenshots.

View Replies View Related

How To Create Genetic Algorithm

May 23, 2015

how to create a genetic algorithm.I made this class that Is supposed to Generate the chromes or Possible solutions of the program. Im pretty sure It doesn't have any syntax error and I went over the logic a couple time yet Im getting a NumberFormatException Exception Error. What this error means nor why am I getting it. Here's my code:

/* Contains the genes or possible solutions to the problem
*/
public class Genes
{
/* Each element is a binary number that corresponds to index number they have been assigned to, these are the possible genes
* The last 4 elements in the array represent + - * / correspondingly
*/
private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

[code]...

View Replies View Related

Using Timer To Close A Class Called By A JFrame?

Nov 26, 2014

I am making a simple battleship program, you have the menu and click Start to get the board with the bombs (4 buttons as of now) Each button has either a bomb or a defualtbutton. I created my button so it can't be unselected. The problem is I have the button in a Class.java and I want that to close in a period of time. What do you recommend using?

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package battleship;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;

[code]....

I can call the clas in my main jFrame but I want to close the class after a certain time.

View Replies View Related

How To Write Instance Method For Rectangle Class Called Contains

May 29, 2014

Write an instance method, contains, that has one explicit parameter of type Rectangle. The method should return true if every point of the rectangle determined by the explicit parameter is on or within the rectangle determined by the implicit parameter. It should return false otherwise.

This is what i did so far?

public boolean contains(Rectangle other) {
Rectangle intersect = Rectangle.intersection(this, other);
if ((intersect.left == this.left) && (intersect.bottom == this.bottom) && (intersect.width == this.width)
&& (intersect.height == this.height)) {
return true;
} else {
return false;
}
}

View Replies View Related

Can't Execute Shell Script Called From Java Class

Apr 7, 2014

My java class doesn't execute a shell sript, I had given the java io file permission read and execute  over the script shell to the user who execute the java class, also I executed the script shell  in the console each RAC server using oracle user session and the srcript shell works. My team DBA told me that " The user server process is oracle". My script shell resides in all RAC server file system the last result looks as follows:

/home/mydir/dir2/mybin/compacta_archivo.sh
No Funcionando!!
java.io.IOException: /home/gi91591/reppostpago/bin/compacta_archivo.sh not found

[Code]....

of course, the script file exist on my file system, it has the suitable permission as the test result i got each time I executed on the console. My Oracle version is 11.2.0.3.0 on Rac ASM.

View Replies View Related

Create Class Employee Which Contains A String Variable Employee Number

Jul 13, 2014

I have searched for totalPay is always 0 and the responses are not related to my problem (that I can tell).This is a class assignment and I have other questions besides why the method is not working.

Here are the instructions: Create class Employee which contains a String variable Employee Number, a String variable Employee Name, an integer Hours Worked, and a double Pay Rate. Create get and set methods for each variable. Provide method "totalPay" which calculates and returns the total pay by multiplying the hours worked by pay rate. Use figure 8.12 as an example of an object with get and set methods.

Unlike other examples of this type question online this one has Employee Number as a String variable. The book is How to Program Early Objects by Deitel 9th edition.We have instructions to do only what she has told us too. At this point it is objects and getter and setter methods. It is only the beginning of week 2.

ON THE LINES WHERE I HAVE QUESTIONS I AM COMMENTING IN ALL CAPS. NOT SHOUTING, JUST TRYING TO MAKE IT EASIER

package test11;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[code]..

Essentially I cannot figure out how to get the method getTotalPay to be called by the Scanner and do the math. I also have this random bracket problem that showed up about an hour ago.

The concept I don't get is that if I omit this.employeeNumber =id; etc. I error all over the place. If I change it to this.employeeeNumber = employee Number Netbeans tells me it is assigning itself to itself, which I know. When I try to use id instead of employeeNumber I get errors all over. If i remove "this" from it the same happens. why in public class employee I have to change to those values (id, name, etc) in the construct and set them again? I don't see where or how they can be used, so why do i have to do it?

View Replies View Related

Possible To Create Program With JAVA That Uses Algorithm To Summarize Data Input

Jan 23, 2015

So I was just wondering if it's possible to create a program with JAVA that uses an algorithm to summarize data inputed. I've never done something like this before, If this is not possible with Java is it possible with C++ or any other language??

View Replies View Related

How To Print Big Integer Using For Loop

Mar 6, 2015

I amtrying to iterate a value of L,R both are range of BigInteger but its not working

BigInteger L=in.nextBigInteger();
BigInteger R=in.nextBigInteger();
for (BigInteger bi =L;
bi<=bi.compareTo(R);
bi = bi.add(BigInteger.ONE)) {

//Task to do with Numbers
}

I am trying to iterate a for loop in range of L and R but its not working

View Replies View Related

How To Make Print Employee Button To Print Info Of Other Class

Apr 12, 2014

Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.

The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.

company class:

import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Company {
private ArrayList<Employees> list ;
private static int numberOfCompanies;

[Code] ....

error message when print button is clicked:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
at javax.swing.JOptionPane.createInternalFrame(Unknown Source)
at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)

[Code] ......

View Replies View Related

JavaFX 2.0 :: How To Create Off Screen Images By Canvas In Multi-thread Environment

Feb 25, 2015

I want to create off-screen images by Canvases in multi-thread environment.
 
I know that I use methods of GraphicsContext2D to draw on Canvas. Can I do this on Canvas which is not added to Scene ?
 
How can I get an image from a canvas after I invoke GraphicsContext2D methods? Can I get images in multi-thread environment ?

View Replies View Related

How To Create Simple Random Number Generator For Number Between 1 -10

May 18, 2015

I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number.I have used google, but they are using LOG statements, what it does.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved