As one of the methods of my IntTree tree I have to implement a method that multiplies the level number with the sum of the nodes on the current level. So far I have this, and it doesn't work. What I am wondering is am I on the right track at all with the second return statement?
public int depthSum(){ return depthSum(overallRoot); } private int depthSum(IntTreeNode root) { if(root==null) return 0; int level = 0;
I am trying to create a method that calculates the standard deviation of array. What I want to be able to do is something like this
package standardDevaitionAndMean; public class StandardDeviationTest { public static void main (String[] args) { int [] array = {12,12,12,12,12,12,12}; standardDev = array.StandardDevation(); System.out.print(standardDev); } }
With something like this
package standardDevaitionAndMean; public class StandardDeviation {double mean1; double standDev; public double Mean(double ... array) { for(int i=0; i<array.length;i++)
[Code]...
So basically I want to be able to make an array in a class and be able to calculate its standard deviation with my method in my other class. I know the code I wrote is terrible but I just wanted to show what I am trying to do. I am kind of shaky on how arrays operate
I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below
public static double Series(int n){ if (n==0)return 0; else return (n/(n*2+1)) + Series(n - 1); }
However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.
I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?
XML Code:
package recursivecall; import java.util.Scanner; /** * Author: <<Conrado Sanchez>> Date: Task: */ public class RecursiveCall {
a)Write a method that recursively displays any given character the specified number of times on one line.For example, the call: displayRowOf Characters(,5);
Produce a line: *****Write another method that uses a for-loop to perform the same process.
B is something like this ? for (i=1; i<=n; i++) i= '*' * n; System.out.print(i);
I succeed but with limit. the code is below. I cant use advanced material (really just the basics even without loops)
if statements are allowed, and i must use Final variables for key value.
I must use one class and one method (the main}|). I need to prompt the user for a entrance time and exit time, it must be with a space , like 18 00 and not 18:00
Also, if the entrance time is greater or equal to 18 00 the charge is always 20 dollars. If the user enters wrong data or out of range data an output of "ERROR WRONG DATA" and then the programs terminates. Same for wrong time . like if the enter hour is bigger than the exit hour an output of "ERROR entrance time must preccede exit"
for the first hour the output is "no charge", for the second hour the charge is 10 dollars. from the 3 hour for every 15 minutes an additional fee of 3 dollars.
for example if the user entered 10 00 and 13 20 , it should be 52 dollars
** it works for me but if the difference between the entered and exit hours is more then 3 it doesnt work. I could've added another if statements for the bigger hours but it will be very long.
I managed everything. except the last part. That calculates additional 3 dollar for each 15 minutes. what is did is.
converted everything to minutes.
subtract enter time from exit time.
and divide by 15.
thats give me the number of 15 minute periods.
if it last or equal to 4. no charge
else last or equal to 8 . 10 dollars
else...... here is the hard part.
if the total minutes wont divide even by 15 it wont work.
Its a program that calculates Fibonacci number.This program uses recursion.
import java.util.Scanner; public class FibonacciMemoization{ static int[] fib = new int[60]; public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a Number :"); int number = input.nextInt();
My program never compiled. the code is for a dialog box that calculates the volume of a cylinder, and i know there are simpler codes to do it, but this is the format my teacher wants...Heres the code:
import javax.swing.*; public class classTwo { public static void main(String[] args) { String rad, hei, vol; Scanner joe = new Scanner(System.in); System.out.println ("Hi, please enter a radius."); double rad = joe.nextLine(); System.out.println ("Enter a height.");
I am having trouble compiling my code for a java application that is supposed to print out the infinite series for Pi. Here is my java code:
//a java application that generates the infinite series for Pi, ie 3.14159...=4-4/3+4/5-4/7+4/9
public class Pi{ public static void main(String args[]){ //declare and initialize variables long counter=1; double pi;
[Code] ....
Here is the error in my output that results when I attempt to compile my code:
C:UsersanonymousDesktopchapter five exercises for javaPi.java:21: error: variable pi might not have been initialized total=total+pi; ^ 1 error Tool completed with exit code 1
Why do I need to initialized pi when I already initialized the total?
There is an error in my code that pops when I tried to execute my java application for an exponent function that I wrote. Here is my code and here is the error is generated when I run my code:
//a java application that calculates the power of an integer in a method that the user creates //and uses a while loop to do so. User cannot use any Math class methods to perform this calculation
import java.util.Scanner; public class exponentiation{ //main method begins execution of java application public static void main(String[] args){
[code]....
Why is it an illegal start of an expression? I didn't think you needed to used if statements for a whileloop.
1.Write a Java Program that calculates the change for a cashier. The program requests the cost of the item. The user then types in the cost. The program then outputs the cost of the item including the sales tax (use 6% as the sales tax value). The program next requests and recieves the amount tendered by the customer. Finally, the program outputs a summary of all figures, including the amount of change due to the customer. Name the program as Change.java.
This program has the following attributes and methods:
2. Create a java program named Numbers whose main() method holds two integer variables. Assign values to the variables. Pass both variables to methods named sum() and difference(). Create the methods sum() and difference(); these compute the sum and difference between the values of two arguments, respectively. Each method should perform the appropriate computation and display the results. Name the program as Numbers.java.
I need getting started designing and creating a program that calculates simple and compound interest. Simple interest in this case means that interest is only applied to the original amount. For instance, if a person deposits $1000 at 10% annual interest, then after one year they would have $1100 (the original $1000 plus the $100 interest) and after two years they would have $1200 (the original $1000, plus the $100 earned the first year, plus the $100 earned the second year).
With compound interest, the interest is applied to all money earned. So, starting with $1000 at 10% annual interest, after one year the user would have $1100 (the original amount plus $100 interest) and after two years the user would have $1210 (the $1100 they started with at the beginning of the year plus the $110 interest).
The requirements of this program are:
-Create a class that will hold methods to calculate the interest. Do not include the main method in this class. -In the class that is used to calculate the interest, include a method to print to the screen, by interest period, the starting amount for the period, the interest earned for the period, and the total amount at the end of the period. Note that each period represents the time frame for how often it is updated. If annual is selected the period is every year. If it is semi-annual it is every six months. If it is quarterly it is every three months. -Create a demo class that will use your interest calculation class. Prompt the user for the original amount, the type of interest (simple or compound), the annual interest rate, and whether it is updated annually, semi-annually, or quarterly. Use method calls to your calculate interest class to do all calculations and display the result. Loop the program until the user chooses to quit.
Having just started college, Bob has been busy looking for a part-time job to fund his new college social life and after only two weeks of looking he has managed to get two job offers! Each job comes with different hours, basic rates of pay and over-time rates so he needs to work out which would get him the most money.
Develop an application that would allow Bob to enter his basic pay rate, his number of regular hours work per week and his number of overtime hours per week. The application should then calculate and display Bobs total basic pay for the week, his overtime pay for the week and his total pay including overtime.
Your application should use instantiable classes to separate the calculations from the user input and output.
Save the instantiable class as Pay.java
Note: The overtime rate is 1.5 times the basic rate of pay
This is my instantiable class code which compiles correctly
public class Pay{ //Declare Data members/Variables private double pay; private double hours; private double overtime; private double totalBasicPay;
[Code] .....
My errors in the second bit of code that i have never seen before
"C:UsersAndreDownloadsPayApp.java:25: error: no suitable method found for println(<null>,String) System.out.println(null,"Please enter Basic Pay"); ^ method PrintStream.println() is not applicable (actual and formal argument lists differ in length)
I wanted to write a simple code that receives information from the user (double-digit number and above) and calculates it's numbers.
For instance- The user wrote a number- 234, the software will return the number 9 (2+3+4 = 9).
Tried to use a loop for that, but I got stuck since I didn't know what to write.
This is what I've wrote so far
Java Code:
import java.util.Scanner; public class Calculate { public static void main(String[] args) { System.out.println("Please type a number larger than 9"); Scanner type = new Scanner(System.in); String number1 = type.nextLine();
for( int i = 0; i < number1.length(); i++ ){ int calculate = number1.charAt(i); } } } mh_sh_highlight_all('java');
i am very new to java programming what i am doing wrong here. Write a program that calculates how much an employee would earn over a period of time (in months), if, every month, the employee’ pay-per-hour rate is one dollar more than the month before (so if his starting perhour rate is 7.25, next month it will be 8.25, next month 9.25, and so on). The employee is going to work 20 hours per week, 4 weeks per month.
The program should input from the user and validate both number of months (which should be a positive integer larger than 0) and the pay-per-hour rate (which should be a floating-point number larger than $7.25 – the minimum federal wage pay-per-hour rate). For each one of these values; if the value is wrong, the program should repetitively ask for that value until the value entered is correct. The program should confirm/output the correct value.
/* This program calculates how much as employee would earn if every month the employee pay per hour would increase by a dollar */ package nick.employee; import java.util.Scanner; public class nickemployee {
Write a Java program that calculates the area and volume of a cube, sphere, cylinder, and regular tetrahedron. Your program should obtain the necessary input for each of the objects from the console and output the input, area, and volume of the object. Use appropriate messages to guide the user of your program.
Here what i did, i am not sure if this is what the assignment want, but this is the best method i could come up with
//Khang Le import java.util.Scanner; public class InputAreaVolume { public static void main(String[] args) {
I'm trying to implement a non-recursive version of the insertion method, but I'm having a bit of trouble. From what I can tell, it's only returning the last two node..
public void insert(Key key, Value val) { root = insert(key, val, root); } private Node insert(Key key, Value val, Node x) { if(x == null) { x = new Node(key, val, 1);
I have a requirement where I have a class as Page which itself contains ArrayList<Page>.Here ArrayList<Page> is nothing but the pages which are accessible from the base Page.I know the depth level ( reading from file) which means how many level I need to go to identify more pages.BUT the problem is how to set the base Page class. I need to set the base Page class but for that I need the objects for the subsequent pages and hence an iterative type of implementation.