Program That Computes Factorials Up To 50?

Mar 8, 2014

I have to write a program that computes factorials up to 50

public static void main(String[] args) {
int [] a= new int [100];
for(int n=0; n<=50; n++) {
double factorial = 1;
for (int multiplier=1; multiplier<=50; multiplier++)

[Code] ....

I have this as my code so far I just don't know how to incorporate the array in there which i need to do as well as I dont get why all the output comes out for every single thing . IT will write 1 factorial and display all the factorials up to 50 and do the same pattern over and over again.

View Replies


ADVERTISEMENT

Java Program That Computes Tax And Tip On Restaurant Bill

Feb 4, 2014

I am trying to do this program. it compiles and everything but the numbers are not coming out right. Heres the problem.

Write a program that computes the tax and tip on a restaurant bill. The program should use JOptionPane to ask the user to enter the charge for the meal. The tax should be 6.75 percent of the meal charge. The tip should be 15 percent of the total after adding the tax. Display the meal charge, tax amount, tip amount, and total bill on the screen.

import javax.swing.JOptionPane;
public class Bill {
public static void main(String[] args) {
double mealPurchase = 0;
mealPurchase = Double.parseDouble(JOptionPane.showInputDialog(nul l, " Enter meal total: "));

[Code] .....

View Replies View Related

Sum Of Factorials

Feb 10, 2014

The first method returns the factorial of an integer (factorial of 3 = 1x2x3). And the second returns the sum of the factorials between 2 integers. This is correct according to my professor. I'm just confused as to why in the 2nd methods it isn't "total = total + Factorial(i);" instead of what I have.

public class LoopProblems
{
public int Factorial (int n){
int total = 1;
for (int i = 1; i <= n ; i++ ) {
total = total * i;
}
return total; 
}
 public int SumOfFactorials (int start, int end) {

[code]....

View Replies View Related

Implement Class That Computes All Primes Up To Some Integer N

Feb 5, 2014

I am working on a problem that computes primes. Here is the problem: You are going to implement a class that computes all the primes up to some integer n. The technique you are to use was developed by a Greek named Eratosthenes who lived in the third century BC. The technique is known as the Sieve of Eratosthenes. The algorithm is described by the following pseudocode:

create a queue and fill it with the consecutive integers 2 through n inclusive.
create an empty queue to store primes.
do {
obtain the next prime p by removing the first value in the queue of numbers.
put p into the queue of primes.
go through the queue of numbers, eliminating numbers divisible by p.
} while (p < sqrt(n))
all remaining values in numbers queue are prime, so transfer them to primes queue

You are to use the Queue interface provided. When you want to construct a Queue object, you should make it of type LinkedQueue. These classes are included. You should define a class called Sieve with the following public methods:

Sieve() - Constructs a sieve object.

void computeTo(int n) - This is the method that should implement the sieve algorithm. All prime computations must be implemented using this algorithm. The method should compute all primes up to and including n. It should throw an IllegalArgumentException if n is less than 2.

void reportResults() - This method should report the primes to System.out. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method. It is okay for it to have an extra space at the end of each line.

int getMax() - This is a convenience method that will let the client find out the value of n that was used the last time computeTo was called. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.

int getCount() - This method should return the number of primes that were found on the last call on computeTo. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.

Your reportResults method should print the maximum n used and should then show a list of the primes, 12 per line with a space after each prime. Notice that there is no guarantee that the number of primes will be a multiple of 12. The calls on reportResults must exactly reproduce the format of the sample log. The final line of output that appears in the log reporting the percentage of primes is generated by the main program, not by the call on reportResults.

Here is my class Sieve. I am having difficulty getting all the primes into the proper queue:

public class Sieve {
private Queue<Integer> primes;
private Queue<Integer> numList;
private boolean computed = false;
private int max;
private int count = 0;

[Code] ....

When I input say, 20, I only get 2, 3, and 11 back as primes. Somewhere in the algorithm (lines 40-54) I seem to have gone awry, but I'm not certain where. Here are the other classes:

SieveMain:
// This program computes all the prime numbers up to a given integer n. It uses the classic "Sieve of Eratosthenes" to do so.

import java.util.*;
public class SieveMain {
public static void main(String[] args) {
System.out.println("This program computes all prime numbers up to a");
System.out.println("maximum using the Sieve of Eratosthenes.");
System.out.println();

[Code] .....

View Replies View Related

Calculating Factorials Using BlueJ

Oct 7, 2014

I am trying to calculate factorials using BlueJ. All of my factorials calculate correctly, I am just having an issue with something the instructor asked of us. She asked us to force the loop to stop when the user inputs "Calculate the factorial of 0", and not give any print.

So far I have my for loop with the correct conditions, I am just really confused as to how to make an if statement to stop the code when the input is 0.

View Replies View Related

Finding Factorian - Sum Of All Factorials

Apr 10, 2014

I have written the following code to try and find a factorian but it doesn't work all the time. A factorian is supposed to be the sum of the factorials.

public static boolean isFactorion(int n){
boolean rv = false;
int sum =0;
int fact = 1;
for(int i = n;i>=1;i--){
fact = fact * i;

[Code] ......

View Replies View Related

Cash Register - Total Up Sales And Computes Change Due

Mar 4, 2015

I'm writing this super simple code as I follow along in this java textbook, and I don't know why it's giving me an error in my tester program!

CashRegister.java

public class CashRegister //a cash register totals up sales and computes change due
{
private double purchase;
private double payment; /**establishes instance variables "purchase" and "payment" and assigns them as doubles to store the
two amounts later on. the assignment as private makes it so ONLY methods in this class can access
these instance variables*/

[Code] .....

View Replies View Related

Calculate Factorials Of Numbers 1 Through 10 - Validating Input

Sep 13, 2014

I'm trying to write a program that calculates the factorials of the numbers 1 through 10, based on user input... My problem is that I don't know how to address the possibility of the user entering something other than a number. When I test the following code by entering a letter, I get an Input Mismatch exception. I'd like to be able to inform the user that the entry is invalid, and ask for another response. Here is my program thus far:

Java Code:

import java.util.Scanner;
public class Factorial {
public static String entryString;
public static char entryChar;
public static Scanner input = new Scanner(System.in);

[Code] ....

View Replies View Related

Create Little Program Which Enter Number / Program Says Triangle Exist Or Not

Mar 16, 2014

i want create little program which enter the number, ant program says triangle exist or not. So code :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[code]...

So when i put first num like 1, next num like 2 and next num like 100 , program says Triangle exist.

View Replies View Related

Program To Open Excel Sheet From Java Program

Apr 16, 2014

Have written a program to open Excel sheet from java program.Below line works fine.

Process p = Runtime.getRuntime().exec(new String[]{""C:Program Files (x86)Microsoft OfficeOffice12Excel.EXE"","C:UsersRASHPA~ 1.ORAAppDataLocalTempExport_xl420314062726 9379706.xls"});

But below code gives error i.e. Executable name has embedded quote, split the arguments

String path = "C:Program Files (x86)Microsoft OfficeOffice12Excel.EXE";
String file = "C:UsersRASHPA~1.ORAAppDataLocalTempEx port_xl4203140627269379706.xls";

Process p = Runtime.getRuntime().exec(new String[]{"""+path+""" + ","+file});

I am using java 1.6.

View Replies View Related

Program That Links Several GUI As Menu Based Program

Dec 17, 2014

In a project for school. I have a program that links several GUI's as a menu based program. What I am trying to accomplish is when one of the previous GUI's is closed that it doesn't terminate the entire program. There is a lot of classes in the entire project so I'd prefer not to paste all the code here, but if it is necessary I will do so.

View Replies View Related

Creating A Program That Will Compile And Run Another Java Program

Feb 20, 2014

I'm creating a program that will compile and run another java program:Lets say I have a program in directory

D:HelloWorldsrc
and compiled program will be in
D:HelloWorldin
inside src and bin is a folder hello (that's a package)

package hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
System.out.println("Hello World");
}
}

This program will be run by another program (that's the program that I am creating).Here is the code of my program:

package runnercompiler;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class RunnerCompiler {
 
[code]....

View Replies View Related

How To Restart Math In The Program Without Getting Out Of Program

Feb 15, 2014

I'm working in a GUI program, but I'm not going to put the code because there is a lot of code and files. Instead, I will try to put it an example.

Let say:

I'm working in a GUI program that ask form the user to enter two number's. When the user press at the calculate button. It will show up the output. However, the program won't exit unless the user press at red (X).

int x = user_Input1;
int y = user_Input2;
int total = x + y; //  
JOptionPane.showMessageDialog(null, total);

I know that there will be a (total) now, so my question is here how can I reset all the calculation and have a new total that will show up when the user enter two number's again.

View Replies View Related

How To Keep Int For Program

Jun 18, 2014

I have searched the internet for about 2 days and now i'm posting this. I'm trying to make a program that will let me enter "1" to show how many movies I have watched and press "2" for a new one. The problem I ran into is I enter 2 then when I enter 1 it said "you have watched 0 movies". I know what the problem is I just don't know how to retype it. here is the code.

package stuff;
import java.util.*;
public class thing {

[Code].....

View Replies View Related

How To Add Images To Program

Dec 14, 2014

I just finished a tutorial on youtube that covers the basic java, and now i want to try and make a game / program thing, only problem is that i have never worked with graphics before (i have tried to make ovals, rectangles, and other things with the paint method tho )

I know how to make a jframe and a jpanel, and i feel like i can make a simple "game" (not a complicated one, just a simple one with a guy walking around on the screen) in fact i have already make a such game, where you are a oval walking around on the screen, so thats not really my problem.

My problem is that i want to draw an image on the screen that can walk around instead of the oval, but how to do that. I have made a new project and set up the jframe and jpanel, and my code looks like this:

(main)

import javax.swing.JFrame;
 public class main_class {
public static void main(String[] args){
 JFrame f = new JFrame();
f.setTitle("title");
f.setVisible(true);
f.setSize(800,600);

[code]...

but once again, i get a blank jframe with no image and no "test" string . How i can display images on the screen?

View Replies View Related

How To Repeat Program

Feb 7, 2015

I have done a rock, paper, scissors program, but it only executes once. How do I make the program repeat so you can play multiple times?

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
 
int A;
 
Scanner input = new Scanner(System.in);
Random random = new Random();

[code]....

View Replies View Related

GPA And Average Program

Mar 25, 2014

I am new on this and I will be working in a gpa and average program. This is my code so far: Every time that I try to add the gpa part it gives me errors or just duplicates whatever I've in average and displays it in gpa

/**
* This program shows a database of students in a school.
* It gathers information as name, classes, grades, and calculates average and gpa average.
*
*/
import javax.swing.JOptionPane;
 
[code]....

View Replies View Related

Program That Estimates Value Of π

Oct 15, 2014

In this exercise you will write a program that estimates the value of π. Pretend that we are looking at a dartboard in the picture above. If the square is 2units wide and 2 units high, the radius of the circle within the square is 1 unit. Then the area of the circle is π*r2 = π*12 = π. The area of the square will be 2*2 = 4.

The estimation of π will work by randomly "throwing darts" at the dartboard. We will assume that every dart will hit the board but the location of that strike will be random so some darts will land inside the circle and some will land outside the circle. The calculation of π is then the ratio of the number of darts that land inside the circle to the total number of darts, multiplied by 4. Note that as we increase the number of darts used in the simulation, the accuracy improves.

Follow the steps to complete the program:

1.Declare variables that represent x, y coordinates of a dart, the distance of the dart from the origin, the number of darts that land inside the circle, and the total number of darts.

2.Read in the total number of darts (N) from the user and create an instance of the Random class.

3.Create a loop that runs N times. Inside the loop, you will use the nextFloat() method of the Random class to create x, y coordinates randomly, and then compute the distance of the dart from the origin. Determine whether the dart lands inside or outside of the circle.

4.Compute π using the estimation algorithm.

5.Run the program with increasing numbers from 100 to 100,000,000 and observe the accuracy of π.

View Replies View Related

Using Sentinel Value To End A Program?

Mar 24, 2014

So the question asks us to use a sentinel value to end a program, but the sentinel value is 999, but the while loop only recognises strings...
 
static Scanner console = new Scanner (System.in);
public static void main (String[] args)
throws FileNotFoundException 
{
PrintWriter outfFile = new PrintWriter ("Invoice.txt") ;
String firstItem;
String item ;
int price;

[Code] .....

View Replies View Related

Program With Hashset

Dec 10, 2014

A file has the data of the subjects and name of professors. The file contains line for each subject. The line starts with professor Name followed by the Subject name.

Harry Williams Advanced DBMS

James H Computer Networks

Sasha Ben Artificial Intelligence

Harry Williams Software Engineering

Palbo Kastro Formal Languages

Alex W Advanced SE

James H Operating System

Harry Williams Theoretical Foundation

Write a program with setter getter method to read the file, and then write a class to store several professors in a hashset (Single Key and Multiple Values).The program should be able to display all the professors in the set and allow the user to search for a professor.

Input:

Professor Name: James H.
Then the Output will be:

Subjects Taken by the professor: Computer Networks, Operating System.Display No Classes available if the professor name does not exists .

View Replies View Related

GUI Program Terminates Itself Once Run?

Aug 22, 2014

This program will compile. Yet once i run it it immediately terminates itself....

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class myImageIcon extends JFrame{

[Code] .....

View Replies View Related

Getting Rid Of Redundancy In Program

Jan 15, 2015

The objective of the program: Write a method that takes a string as input and returns the letters contained in that string in sorted order as output. For example, an input of "Computer Science" would print the following as output (case is ignored): ccceeeimnoprstuthe code works otherwise

import java.util.Scanner;
public class weeklyProblem4 {
public static void main(String[] args) {
Scanner console = new Scanner (System.in);
System.out.println("This program takes a string as input and returns the letters contained in that string in sorted order as output");
System.out.println();
System.out.println("Enter some string");
String userstring = console.nextLine();
sortedString(userstring);

[code]....

View Replies View Related

How To Put Search Bar In Program

Oct 21, 2014

how to put search bar in my program.i am just new to java programming so i dont really master all the codes.i dont have much time

import java.io.*;
import java.util.Scanner;
public class myjavProject{
public static void main(String[] args)throws IOException{
PrintWriter output = null;
String ID;
String firstname;
String lastname;
String ans = "y";
String userinput;

[code]....

View Replies View Related

Pi Approximation Program

Sep 4, 2014

I am new to programming and this is my first assignment. A sample of the final output is:

-"How many digits would you like to compute Pi to?" 2
-"It took 200 terms to approximate Pi as 3.136592684838816"

What I have so far, ask the user for input then stops!

import java.util.Scanner;
public class PiApproximation
{
public static void main(String[] args)

[code]...

View Replies View Related

Program Terminates When Try To Run It

Mar 7, 2015

I have to create a class that asks the user to answer a multiplication problem. It randomly pulls to integers. If the answer is correct it displays a response if its wrong it displays a response and tells them to try again. at the end it displays all the questions with the answers. For some reason when i run the program it automatically terminates but doesnt give me an error.

Java Code:

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
//import javax.swing.*;
public class StudentMath {

[code]....

View Replies View Related

Tax Calculation Program

Apr 9, 2014

use arrays to store taxpayer information. Use methods for tasks that will be repeated.

-Ask the user how many taxpayers he would like to calculate taxes for.
-Ask the user to enter each taxpayer's first name, last name gross income, and number of children.
-Each taxpayer's tax due is computed as follows
-The taxpayer's dependency exemption is determined by multiplying $3,000 times the number of children.
-The taxpayer's net income is determined by taking the taxpayer's gross income and subtracting the taxpayer's dependency exemption.
-If the taxpayer's net income is between 0 and 50,000, the tax due is 15% of net income.
-If the taxpayer's net income is greater than 50,000, the tax due is
-15% of the first 50,000 of net income PLUS
-25% of any income over 50,000
-The tax due can never be less than 0.
-If the net income is a negative number, the tax due is 0

TAXPAYER INFORMATION: output a message in one dialog box which lists the following info for every taxpayer: first name, last name, gross income, number of children, tax due.
AVERAGE TAX: output a message in a dialog box which states the average of the taxes due.
PRESIDENTAL MESSAGE: output a message in a dialog box which says either "We computed taxes for the president. " or "We did not compute taxes for the president." The president's name is Barack Obama.

Here is my code so far

import javax.swing.JOptionPane;
public class AssignmentSeven
{
public static void main (String [] args)
{
String [] taxPayers;
String[] firstName;
String [] lastName;
String message = "";
double[] grossIncome;

[code]....

View Replies View Related







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