Using Loops To Do Multiple Divisions
Feb 2, 2015
I am trying to write a program that will take two inputted numbers (a and b) and determine how many divisions can be done by dividing by b until the quotient is less than b. For example if a=64 and b=2, then you would be able to do:
64/2=32
32/2=16
16/2=8
8/2=4
4/2=2
2/2=1
1<2
so there is 6 divisions.
How would I use a loop to create this type of program?
View Replies
ADVERTISEMENT
Jul 18, 2014
I've beginning a new assignment and would like some feedback on my outline for the program. You are to design a simple calculator using the stack data structure to perform additions, subtractions, multiplications and divisions. The user may enter an arithmetic expression in infix using numbers, parentheses and arithmetic operations (+, -, *, /). After an expression is entered, its postfix or prefix notation is displayed and then the result of the expression. You are required to design your own infix to postfix and infix to prefix conversions.
The professors example includes: SinglyLinkedList implementation, a Stack that implements Stack interface and extends SinglyLinkedList, a Node class, and StackApp -- in addition to the main class and the prefix/post-fix classes.
(The equation is entered/stored as a string variable)
System.out.println("Enter equation: ");
equation = input.next();
equation.trim();
(1.) Get the user input:
- evaluate the user input and use a stack to determine the correct prefix/post-fix notation.
- from the stack, pop each item:
a. account for '(' and ')' to push all operators and write all operands until ')', then pop/write
b. store new equation into a list (in prefix/postfix notation)
(2.) Display the list in both notations to the user.
(3.) Perform the calculation.Do you think it will be alright to calculate the original equation that was entered? It seems too much to then design a class that will then cycle through the polish notation and calculate according to that format.
View Replies
View Related
Dec 8, 2014
I have i am trying to implement tooltip through javascript, like when we click on an image link tooltip should be displayed and it should have close button/ close image to close that tooltip.like the same way i will have multiple images on page, when ever i click on the images all tooltips should be displayed on the page when ever i want to close that then only it should close through close button on tooltip.can we do it through java script or will go for jquery.
View Replies
View Related
May 5, 2015
I'm very new to Java, and I am creating a program that takes multiple user input to create one face. I have a class for the eyes, nose, lips, and headshape. For some reason, my program is not drawing the graphics. ***for question purposes, I have only included my head shape class and my test class****
my "test" class:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FaceTest
{
public static void main(String[] args)
{
String head = JOptionPane.showInputDialog("Would you like a circle, square, rectangle shaped head?: ");
[Code] ....
View Replies
View Related
Dec 5, 2014
I'm working on an old site that we have, I'm adding a couple dropdowns to our forms.With a connection to our database I can pull in values for selBaseCurr, But it's not pulling any values into the second dropdown for selTargetCurr...Am I missing something in the while loop?
<select title="Select currency to convert from" id="selBaseCurr" name="selBaseCurr">
<% while(rs.next()){%>
<option value=""><%= rs.getString("CURR_CODE") %></option>
<%} %>
</select>
<!-- /base -->
[code]....
View Replies
View Related
May 28, 2014
here I have a do-while loop. When I push a button, then press enter, the program should do stuff, then ask me for another key. If I press any key but q, it repeats itself. If I press q then enter, it should end the loop. But then when I press q then enter, it still does the functions in the do-while loop.
Java Code:
Scanner kb = new Scanner(System.in);
System.out.print("Press any key to start.");
String letter = kb.next();
do
{
stuff
System.out.print("Press any key to call another number or press "q" to quit.");
letter = kb.next();
}while(!letter.equals("q") || !letter.equals("Q")); mh_sh_highlight_all('java');
View Replies
View Related
Dec 19, 2014
What I'm trying to do below is to say if (adultTickets > 0) I want to bypass the studentOAP tickets question and go straight to the question about dinner. But if (adultTickets ==0) I want to go to the studentOAP question and then if the (studentOAPTickets >0) to go to the question about dinner. But if the (studentOAPTickets ==0) I want to go straight to the question about the contact number.
System.out.print("How many adult tickets do you require? ");
int adultTickets = 0;
boolean validAdultValue = false;
while (validAdultValue == false) {
if(aScanner.hasNextInt())
[Code] ....
View Replies
View Related
Nov 21, 2014
i'm confused about the if statement part because int i = 0 is set to zero but how can you get the remainder if int i is zero?
for (int i = 0; i < 200; i++) {
if (i % 12 == 0) {
System.out.println("#: " + i);
}
explain to why it output like this?
#: 0
#: 12
#: 24
#: 36
#: 48
#: 60
#: 72
#: 84
#: 96
#: 108
#: 120
#: 132
#: 144
#: 156
#: 168
#: 180
#: 192
View Replies
View Related
Jan 3, 2014
So the while loop I am trying to use is:
while( type != "EXIT" ) {
type = input.next();
}
The problem is that typing in EXIT doesn't end the loop like I want it to. I also tried input.nextLine(); but neither of them work. The loop is being used to fill an ArrayList so the number of elements can change based on how many they want. What am I doing wrong, or what alternatives do I have?
Seems I need to use !type.equals("EXIT")
View Replies
View Related
May 15, 2014
how a nested for loop increments the outer loop and then the inner loop and then when it runs the inside code.I've ran the program a few times but I'm just not understanding it, need little narrative to it?
for(int i=0; i < 5; i++){
System.out.println("i - " + i);
for(int j=0; j < i; j++){
System.out.println("j - " + j);
count++;
}
}
View Replies
View Related
Mar 4, 2014
I would like to know which one is more appropriate to use in "for" loops or "while" loops.I don't know exactly when to use Integer or int.
for(int i=0; i<10; i++){ ... }
for(Integer i=0; i<10; i++){...}
View Replies
View Related
May 17, 2015
I am learning while loops but the output I am getting is not what I want? My class:
public class BottlesOnWall {
public static void main (String[] args) {
int x = 99;
while (x > 1) {
System.out.println(x + " bottles on the wall!");
[Code] .....
The output I am getting is this:
99 bottles on the wall!
98 bottles on the wall!
97 bottles on the wall!
96 bottles on the wall!
95 bottles on the wall!
[Code] ....
I want it to stop on "2 bottles on the wall!" and then print "Only 1 bottle left on the wall!". How do I go about doing that? I have tried different variations of the condition (x>1), I have tried (x>5) but it still goes down to 1 bottle?
View Replies
View Related
Jan 16, 2014
I need to build the triangle like below. How to solve this using loops.
*
* *
* * *
* * * *
* * * * *
View Replies
View Related
Oct 29, 2014
I'm trying to make a piece of code that writes a for Loop program using OOP. I need to create a class, name that class and have it contain several reverse methods and use a runner class to test it.So far this is the code I've come up with. Does it work or not?
public class loopDemo{
public static void main(string[]args){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
public String reverse(){
char myBoi;
int myIta;
String tebahpla
for(myIta=25j i>=0 ji++){
tebahpla+= alphabet.charAt(myIta);
View Replies
View Related
Jan 6, 2014
I am relatively new to Java and I am only beginning to learn about SQL. I have some basic's down but I have been wondering is there a way that I can add data to my database using loops instead of having to physically code every row/column individually ?
View Replies
View Related
Mar 26, 2015
how to loop my arrays, so when I sort them (highest/lowest) the 2nd array corresponds to the sorted array
import java.util.Scanner;
import java.util.Arrays;
public class ArraysArrays {
public static void main (String[] args){
[code]....
View Replies
View Related
Sep 11, 2014
I will like replace this code:
// boucle pour les fractions aleatoires
for (i=0; i<2; i++) {
// Creation des fractions aleatoires
Frac first = new Frac(r,5);
Frac second = new Frac(r,5);
[Code] .....
But when I use a switch case I do not have the same result as my four loops.
View Replies
View Related
Oct 5, 2014
The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:
Java Code:
import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args) {
//Declare and initialize variables and objects
//Input: ask user for number of rolls and number of sides on a die
//Print heading for output table
[Code] ....
When I run it, I get something like this (just a part of my output):
Please enter the number of sides on a die: 6
Please enter the number of rolls: 2
Sum of dice Probability
20.0
20.0
30.0
30.0
40.0
40.0
50.0
50.0
650.0
650.0
70.0
70.0
Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here.
View Replies
View Related
Feb 9, 2015
i want to use nested loops to print patterns but i cant seem to make a pattern that will look like V and K this should be the outcome.
* *
* *
* *
* *
*
@ @
@ @
@@
@ @
@ @
View Replies
View Related
Oct 10, 2013
Is there any advantage to using Java's operations defined in sets or collections over inspecting in nested for-loops?
For example, suppose I want to keep items from one array that do not appear in another, I could do:
for( PrimitiveType prim_1 : someArray ){
for( PrimitiveType prim_2 : otherArray ){
if( prim_1 != prim_2){
///put in some return list
}
}
}
Or else I could do:
ArrayList<NotPrimitive> excludedElements =
new ArrayList(Arrays.asList(primitiveArrayToExclude));
for( PrimitiveType elem : someArray){
if( ! excludedElements.contains( elem ) {
//put in some return list
}
}
I personally see no improvement in legibility nor any reduction of error potential.....
View Replies
View Related
Feb 12, 2015
public class dowhile2 {
public static void main(String[] args) {
int i = 1;
while(i <= 100){
System.out.println(i);
if(i == 50){
break;
}
i++;
}
}
}
I just want to know how to count this lines of codes. Because if I place for example i++ further up the code or the system.out.println a bit lower the result will be 1-49 or 2-50 and not the goal that is 1-50.
I want to know how to think and count. Why do i get 2345678.........50 and not 12345678........50 or 12345678.....49, thats the question.
View Replies
View Related
Oct 13, 2014
So I am trying to do this program but I can't figure out how to nest it properly. The prompt is :
Design and Implement an application that determines and prints the number of odd,even,and zero digits in an integer value read from the keyboard.
The catch is, strings are not allowed. This is what I have so far :
import java.util.Scanner;
public class OddEvenZero {
public static void main(String[] args) {
int zero = 0, even = 0, odd = 0, num, num2;
[Code] ....
View Replies
View Related
Sep 10, 2013
I am completing the exercises in Java How To Program 8th Edition by Harvey and Paul Deitel.There are two optional exercises GUI and Graphics Case Study Exercises on page 141 Chapter 4.
I have to use loops and control statements to draw lines thereby creating two designs.I am trying to create the design on Fig. 4.20. The lines do not line up correctly.The lines should fan out from all corners. Individually, they look exactly like it should.
Example of one corner.
int width = getWidth();
int height = getHeight();
// upper-left corner
int startX = 0;
int startY = 0;
int endX = 0;
[Code] ....
For the other corners I have different starting and ending positions.To draw upper-left corner I have a starting position of 0,0 and ending position of 0,height.I then move ending position up one vertical step and right one horizontal step. I repeat this fifteen times.
To draw upper-right corner I have a starting position of width,0 and ending position of width,height. I then move ending position up one vertical step and left one horizontal step.
To draw bottom-left corner I have a starting position of 0,height and ending position of 0,0. I then move ending position down one vertical step and right one horizontal step.
To draw bottom-right corner I have a starting position of width, height and ending position of width,0. I then move ending position down one vertical step and left one horizontal step.
For the second design Fig. 4.21 all my lines line up correctly no matter the direction I resize it. Both designs frame size is 250,250. Both designs are divided into fifteen steps.
View Replies
View Related
Jan 24, 2015
Write a program that will display the following pattern.
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
I need to write a program that uses nested for loops to display that patter but I have no clue on how to do it....
View Replies
View Related
Apr 25, 2015
I am trying to make GPA calculator. I decided to use only switch statement and make it as simple as possible. The point is to enter as many classes and students as user wants. I keep getting credit points 0 at the end and also text format is not good too. Here is the code.
import java.util.Scanner;
import java.io.*;
public class SwitchExercise{
public static void main(String[] args) {
String grade = "";
String grades="";
String coursename="";
String headingleft="COURSES";
[code]....
View Replies
View Related
Oct 21, 2014
I am working on an assignment that is to simulate the relationship between a cache and main memory. Basically it is supposed to be a 16 slot cache and we are to have 500 for main memory. I need it to compare the corresponding index for both arrays, then go through then repeat the cache array and compare it to the next 16 of main memory. It is to simulate an direct mapped cache. How to compare two arrays from separate loops.
public class MainMemory {
private short mainMem[] = new short[200];
public void assign(){
short i;
for ( i = 0; i < mainMem.length; i ++){
mainMem[i] = i;
[Code] .....
View Replies
View Related