Write a program that uses a while loop to perform the following steps: Comment by labelling each part: //Part A, //Part B, etc...
A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20. B.)Output all odd numbers between firstNum and secondNum. C.)Output the sum of all even numbers between firstNum and secondNum. D.)Output the numbers and their square between 1 and 10. E.)Output the sum of the square of odd numbers between firstNum and secondNum. F.)Output all uppercase letters.
Again I am new to while loops and I am totally lost. I have just completed 8 other programs using if else statements and now trying to get the hang of loops.
import java.util.Scanner; public class whileLoop { public static void main(String[] args)
Write a java program that uses a While loop and persistent output to count down from 33 to 0 by threes, and put each value on a separate line, also do this in for loop.
having some trouble doing this. Here are the requirements:
1)Write a program using loop and break to calculate the value of PI. 2)You can use the following equation to calculate PI. PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13 etc.. The program shouldn't loop until the criteria is satisfied. 3)The criteria is the value of PI you get is very close to the “real” value (3.1415926), meaning that their difference should be smaller than a very small numeric number, e.g., 0.0001). 4)The control structures you may need for this program are loop, if-else, and unlabeled break. 5)The output should have the format: for each iteration, it has iteration number and the corresponding value of PI until that iteration. The last line would be the final value of PI.
Here is my code so far:
package ids; public class IDS { public static void main(String[] args) { double pi = 0; for(int i=1; i<=100000; i++){ if ((i%2)==0){ pi+=(4/(2*i-1));
I have a question related to trees in Java. The following code is given:
public class BinarySearchTree { private Node root; public BinarySearchTree() { this.root = null; } public BinarySearchTree(Node root) {
[Code] ....
The inorder-method is the one that really imports. I have to go through a tree using the Inorder-method. I do understand how this works, however I don't understand how the code fits to the inorder process. I understand the code up to the point where it reaches the most left "root", but then I do not know how to go further. How do I reach the root above then?
Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:
public static void main(String[] args) { printChars(‘A’,’z’,10); printChars(‘0’,’9’,5); }
So here is a method i wrote for the first part for initializing an array:The question was, "write a method which initializes a 2D target array (assuming all other classes have been imported and given), and all targets should be stationary."and this was my method i wrote:
public void initializeTargetArray(Target[][] targets) { int row = targets.length(); int col = targets[0].length;
for(int i = 0; i < row; i++) for (int j = 0; j < col; j++) targets[i][j] = new Target(Target.STATIONARY, false); }
Now my question how do I initialize an array similar to the method above for a different question?
Quote Part b Write a method which initializes an array (You can assume that the 2D array itself (but not the Target objects inside) is already initialized), where the game level stage is now set: Game level 1 - all targets are stationary, Game level 2 - For even numbered rows, targets at even numbered columns are stationary, targets at odd numbered columns are movable. For odd numbered rows, targets at odd numbered columns are stationary, targets at even numbered columns are movable. Game Level 3 - All targets are movable.
public void initializeTargetArray(Target[][] targets, int gameLevel) { //*** Finish this method. ***//
I am a student taking a Java programming class. My assignment is to write a program that converts a roman numeral input as entered by a user and converting it to it's integer value (arabic number). These are the methods that I must have in it:
1. Write a method that takes input from the user and passes it to a conversion method. 2. Write a method that yields the numeric value of each of the letters (conversion method). 3. Write a method that outputs the number the user entered and the converted number. 4. Write a main method to test the 3 methods.
I have written the first method, at least I think. Here is what I did there:
public static String romanInput(String number) { Scanner numberInput = new Scanner (System.in); System.out.print("Enter a roman numeral: "); String userInput = numberInput.next(); return userInput; }
I returned the userInput and I think that is passing it to the conversion method? Now I am working on this conversion method and to be honest I don't know where to begin. I am told how to convert a string in my assignment by the professor. I am told:
- Look at the first two characters. If the first has a larger value than the second, then simply convert the first. - Call the conversion method again for the substring starting with the second character. -Add both values. If the first one has a smaller value than the second, compute the difference and add to it the conversion of the tail.
I am also told to use a single-dimensional array. But, I don't know what I am to use a single dimensional array for? So this is what I wrote so far for this method:
I have written a character array for the roman numerals, and then one for arabic numerals, then I set them equal to each other. I also declared an integer variable set to 0 because I think that is what I will be returning at the end of the method. Now I don't know where to start for the conversion algorithm here. I know this is what I have to do, but I don't know how to do it:
1. Add the numbers together if they are in decreasing value or are equal in value. For example: VI is read as 5 + 1 = 6 XVI is read as 10 + 5 + 1 = 16 XXXVIII is 10 + 10 + 10 + 5 + 1 + 1 + 1 = 38 2. Use subtraction if a number is less than the number that follows it. For example, I is less than V, so when I is in front of V, you subtract its value. 3. For example: IV is 5 1 = 4 IX is 10 1 = 9 XL is 50 10 = 40 MCM is 1,000 + (1,000 - 100) = 1,900
I can't use hashtables or enums because I haven't learned about that yet. I have a feeling I need to use a for loop. I know I haven't done any of the actual programming work but I don't know how to begin writing this conversion method.
I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.
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.
How do i write a method in java that will add a <b> or <em> tag to a specific word regardless of case or punctuation for example for "run forest RUN!" adding bold to run would be
<b>run<b> & <b>RUN!<b> public void addTag(String word, String tag) {
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
I am supposed to write a static method that simulates a flip of a weighted coin by returning either heads or tails each time it is called. The coin is twice as likely to turn up heads as tails. My idea was to of course use a random class
Random flip = new random
and somehow initialize it so 3 numbers are called and create if statements so that when 0 and 1 are called heads returns and when 3 is called tails is.How do I put all this together?
So I am calling this method several times and trying to write multiple records to a file. Problem is that every time I call the method it overwrites the file from before and doesn't add it.
public void fileWriterMethod() throws IOException{ RandomAccessFile raf = new RandomAccessFile(filename, "rw"); raf.writeInt(id); raf.writeInt(existingMileage); raf.writeInt(gasCost); raf.writeInt(ndays); raf.writeInt(rate); raf.writeInt(totalCharge); raf.writeInt(discount); raf.writeInt(tax); raf.writeInt(netCharge); raf.writeInt(returnMileage); raf.writeBytes(carName + " "); //Closing the stream raf.close(); }
i have to "Write a method called addToOverThirty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 30 in the array.
Add a call to the addToOverThirty method, then display a message telling what the output is followed by the results For example:The nums3 array after adding 1 to all numbers greater than 30 is10 6 15 and so on (check with the values you assigned to nums3)"its pointless because we were told not to make the array have a number over 30.
import java.lang.*; import java.util.*; public class LastProject public static void main(String[] args) { int nums1[] = new int[15]; int nums2[] = new int[15]; int nums3[] = {5,2,15,8,26,22,1,29,4,23,30,11,19,6,24};
I can't figure out how to complete the for loop for the forward method that needs to calculate each pixel on the path setting it to white, starting at location (x,y) and ending at r.
public class Turtle { private static BufferedImage img; private static int black = 0; private static int blue = 255; // 2^8-1 private static int green = 65280; //(2^8-1)*2^8 private static int red = 16711680; //(2^8-1)*2^16 private static int white = 16777215; // 2^24-1 private double x,y,theta; // what does a Turtle know? static // a static initializer list
My program crashes every time I call this method because of the while loop. I guess its going into a infinite loop. The game should keep running until the user ends it. Is the use of my .isKeyPressed correct and if so what else could it be?
public static void run() { while (true) { if (StdDraw.isKeyPressed(KeyEvent.VK_Q)) { break;
I have created a method Rectangle and added a loop in my class Resizer (MouseAdapter) but impossible to resize the rectangles of the arraylist independantly !
class Rectangle extends Rectangle2D.Float{ private String name; public Rectangle(float x, float y, float width, float height, String name) { setRect(x, y, width, height); this.name = name;
My issue is that when I run my search, it does find a goal. However, when I try and print the route using my print route method, it just gets stuck in a loop of printing the same two nodes. What is wrong with My A* implementation?
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Hours Traveled "); int hoursTraveled = input.nextInt(); System.out.println("Enter MPH "); int mph = input.nextInt();