just trying to learn it on my spare time and I'm doing do-while loops
public class help { public static void main (String args[]) throws java.io.IOException { char choice, ignore; do{ System.out.println ("Choose one:"); System.out.println("1. if"); System.out.println("2.switch");
[code]....
It makes no difference in the program wither i delete this block or keep it..how while (choice < '1'| choice >'2'); translates? I would assume it would be while (choice >= '1'| choice =<'2');?
Question: you are only allowed to use numbers from 1-6. Write a program to find all the permutations when three numbers are multiplied together to give a result 8. one number cannot occur twice in any permutation.
public class number4 { public static void main(String[] args) { for(int a=1; a<=4; a++) { for(int b=1; b<=4; b++) {
[Code]...
my program also prints out 2 2 2. but i'm not allowed to do that. how can I stop it from printing 2 2 2 ?
Here's what "Why doesn't this work?" question. It concerns a small method which is part of a card game.
I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.
Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?
To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.
public boolean isFlanking() { boolean f; int reach = Math.abs(selectorX - targetX); if(rival.getDeck()[selectorX].getPile().isEmpty() == true &&
//Main method public static void main(String args[])throws IOException{ boolean runProgram = true; Scanner keyboard = new Scanner(System.in); //runs program while runProgram is true while (runProgram){
I was told that the answer when this code segment is printed look's like this: $$$$ $$$ $$ $
Here's what I did:Looking at the outer for loop, (i) 0 < 4 so I went into the first inner nested loop. (k) 0 is not less than (i) 0 so I went to the 2nd inner nested loop and found that it worked, and I was able to repeat this loop 3 more times and then I exited the loop and printed the line out (giving me the first line of four $). I then went back to the outer for loop, increased i by 1 and (i) 1 < 4 so I went to the 1st inner nested loop. I used the 0 for the k first and (k) 0 < 1 so I printed out a space (now here's where I get lost) I then incremented k by 1, so k = 1, but 1 is not less than 1 (i) and so I moved on to the next nested for loop. So when this line is printed, I'll only have one space when there should be 4.
Implementation of the nested for loop. I thought it was going to go smoothly but apparently not.Purpose: This program is meant to print out all the prime numbers up to a certain range using the algorithm Sieve of Eratosthenes.
Update output: Works as expected.
Java Code:
import java.util.Scanner; import java.util.ArrayList; public class PrimeFactors { static ArrayList<Double> primeList = new ArrayList<>(); static double range = 0; static double maxPrime = 0; static double primeTester = 0;
I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.
CinemaAppMain public class CinemaAppMain { public static void main(String[] args) { new CinemaApp().start();
[Code]....
I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.
I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).
Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list. (getting the "title" from movie list & "name" from theatre list)
I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...
public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values for (double j = 0; j <= 180;) { for (double i = 1; i <= 5; i++) {//loop to print five lines of code angle = Math.toRadians(j);
I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly. I am stuck on how to divide the filler characters to make the proper square size. As of now they are all one lined.
import java.util.*; public class Checker{ public static void main(String[] args) { int col, row, size; char filler; System.out.println("Please enter 3 numbers and a character."); //output
Now I have to write a method called printDesign that produces the following output but i am not even entirely sure how to start it out now of course i know how to make a for-loop but and i guess if i was to do something for this i would do this .....
Four experiments are performed, each consisting of six tests. The number of test results for each experiment is entered by the user. Write a Java program using a nested loop to compute and display the average of the test results for each experiment.
You can run the program by entering the following test results:
I've been trying to learn more about Big O Notation and I've gotten stuck on a few pieces of code. What is the computational complexity for the following pieces of code?
1:
for(int i = n; i > 0; i /= 2) { for(int j = 1; j < n; j *= 2) { for(int k = 0; k < n; k += 2) { // constant number of operations
[Code] .....
5 : Determine the average processing time of the recursive algorithm. (int n) spends one time unit to return a random integer value uniformly distributed in the range [0,n] whereas all other instructions spend a negligibly small time(e.g., T(0) = 0)
int myTest(int n) { if(n <= 0) return 0; else { int i = random(n - 1); return myTest(i) + myTest(n - 1 - i);
6 : Assume array a contains n values, the method randomValue takes a constant number c of computational steps to produce each output value, and that the method goodSort takes n log n computational steps to sort the array.
I've to create a program to get user input in scientific notation. Now i'm confused that how can i check that how user have used the scientific notation. For example there are many ways to write a number in scientific notation. Also a user can either write a number as:
6.7E2 OR 6.7*10^2
How to handle this input and further convert it to double?
So recently I began Data Structures as core subject and the tutorials in this forum are great.
Right now, I seem to have trouble with Big Oh Notation algorithm and what is the mathematical side to it. "f(n) <= c.g(n), for n>=0.
The question I am working on is: Suppose we are maintaining a collection C of elements such that, each time we add a new element to the collection, we copy the contents of C into a new array list of just the right size. What is the running time of adding n elements to an initially empty collection C in this case?
// the MountainBike subclass has // one field public int seatHeight;
// the MountainBike subclass has // one constructor public MountainBike(int startHeight, int startCadence,
[Code] ....
At first, Java Code: public int seatHeight; mh_sh_highlight_all('java'); tells us that seatHeight is NOT a static field (because of the absence of static keyword).
Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in Java Code: seatHeight = newValue; mh_sh_highlight_all('java'); shows that it IS a non-member/static variable.
My question is to evaluate a Postfix notation entered from keyboard. I have no errors in my code but it prints only :
Exception in thread "main"
java.util.NoSuchElementException at ArrayStack.pop(PostFixEvaluation.java:72) at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107) at PostFixEvaluation.main(PostFixEvaluation.java:140)
I tried many values but it prints the same exception all the time.
So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).
To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.
I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.
The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...
public class Operator implements Comparable<Operator> { public char operator; // operator privateint inputPrecedence; // input precedence of operator in the range [0, 5] privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]
[Code] ....
So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.
My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time