How To Make Sum Of Variables In A Loop
Aug 13, 2014
I've looped a method 14 times, each time it focuses on a different person and how many oranges he/she has eaten. I now want to calculate the total amount of oranges eaten and exactly how many oranges are eaten on average, how can I do that?? the amount of oranges eaten is variable, they are determined by what number the user types in.
View Replies
ADVERTISEMENT
Mar 31, 2014
I am very Java illiterate and I need to know how would i set all the variables in the first class to null or 0 to make a new method to clear everything.
import java.util.Arrays;
import java.util.Scanner;
public class studentMethods
{
private double totalScore;
private int count;
[Code] ....
View Replies
View Related
Sep 6, 2014
Where usually to instantiate and declare variables to be used in a loop. If you declare it outside to be used in the loop it will still be there when the loop is done, never to be used again and is just sitting there taking up memory. However, if you declare it inside the loop, you have the issue of it constantly creating space for said variable, but once it's out of scope it's gone. Is there any advantage performance wise to doing it either way?
View Replies
View Related
Feb 3, 2014
I do not know how to make the loop end. I think there is an issue with my second and thrid algorithms..Also its a commission calculator
--- Update ---
//Start Program
package commission;
import java.util.Scanner;
public class Commission
{
private static boolean saleTargetrange;
[code]....
View Replies
View Related
Feb 18, 2015
I've been learning Java in school for lika a year now. Our teacher wanted us to make a game called "Game of Life" .Now it's not very optimized because the only type of GUI we know how to make it with JButtons so if I want a 100x100 grid that means the program has to load 10000 buttons and change text and color on them which I can imagen cause lags and so on but if we ignore that lagg our teacher wanted us to make it loop-able and well I kinda succeeded but the only problem is that I can't get it to wait between every loop so if I tell the program to loop 100 times, the program processes that and that takes forever and then updates the board and sometimes it takes forever and sometimes you cant even see the "cells" moving. Is it possible to get my program to loop and if so how? Here is the code:
//------------------------------------------------------------------------------------------------------------------------
// Hamta standardpaket
//------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
[code]....
View Replies
View Related
May 8, 2014
We are making a tic tac toe game for my CS120 class and I am having trouble figuring out how to make our X's and O's. Is there a way to make shapes besides making two lines for an X and an oval with a white smaller oval inside to make an O? We have only learned the basics so far in class (i.e. events, inheritance, client-supplier, etc.)
These are our instructions:
Write a controller that controls the game. There is one human player (the X player) and the computer player (the O player). The name of the class must be TicTacToeController. In a sense, the controller is the game since the controller will 1) create a TicTacToeModel 2) create a TicTacToeView and 3) create a TicTacToeButton (you must write this class following the design pattern covered in class lectures), a label, and text field such that when the button is pushed, the player moves into the cell selected by the text field. After every player move, the computer moves into a randomly selected empty cell. When the game is over, a text message must be displayed somewhere on the screen the gives the status of the game. While you are free to change the appearance of the controller, the basic elements must be provided (a view of the game, a button, and a text field to enter the cell). A sample screenshot is displayed below.And this is the code i have thus far:
[import java.awt.*;
import javax.swing.JFrame;
public class TicTacToeView extends Rectangle
public TicTacToeView(int x, int y, int w, int h) {
super(50,60,w,h);
this.setBackground(Color.red);
JFrame win = new JFrame("Tic Tac Toe");
win.setBounds(10,10,w+100, h+100);
win.setLayout(null);
win.setVisible(true);
win.setBackground(Color.gray);
[code]....
View Replies
View Related
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related
Feb 25, 2014
Here's the code: it's while loop inside a for loop to determine the proper length of a variable:
for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "
[code]....
I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.
View Replies
View Related
Nov 2, 2014
I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.
Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.
After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.
The output of the program should something like this:
John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67
Code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
[code]....
View Replies
View Related
Mar 8, 2014
How to convert this program from a while loop to a for loop.
import java.util.Scanner;
public class LongDivision {
public static void main(String arguments[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the dividend: ");
[Code] ....
View Replies
View Related
Feb 17, 2014
How do I convert my for loop below to a while and do while loop?
for(int a=1;a<=9;a++){
for(int b=1;b<=a;b++){
System.out.print(a);
}
System.out.println();
[Code] .....
View Replies
View Related
Feb 9, 2015
I am trying to make a program add values from a loop. So what its supposed to do is search through tokens on an imported file. The file lists State, Capital, and then capital population. Then take the population string, turn it into numbers, and then do stuff with the numbers. First I'm supposed to find the Highest and lowest population of the places in the file (which I did without problem), but the finally thing is I'm supposed to add each found population to the last so I can find the average of the populations.
I just cannot seem to grasp how to do that. I THINK I'm supposed to some how store the given value into a variable, but how do I get that variable to add to the new value?
like...?
Get token -> a
b = a
c = a + b
or wait no.....
Java Code :
import java.io.*;
import java.util.Scanner;
public class CapPopS
{
public static void main(String[] args) throws IOException
{
File stateCAP = new File("state-capital-2004population.txt");
if (!stateCAP.exists())
[Code] ....
View Replies
View Related
Mar 30, 2014
Let's get it out of the way -- I'm stumped. On something that should be pretty simple to solve. Code follows.
public class Sum {
public static void main(String [] args) {
Double nSum = 0.0;
Double aDouble = 100.0;
for (int i = 0; i < 1000; i++){
nSum += 0.1;
[Code] ....
The output:
100.000000
100.000000
false
false
false
false
false
Process finished with exit code 0
I wrote another simple program and hardcoded the values: aDouble = 100.0; bDouble= 100.0000
Using aDouble.equals(bDouble) returns true, just as one would expect.
So what am I overlooking?
View Replies
View Related
Feb 25, 2014
Having two values
<c:set var="var1" scope="view" value="#{ID1}"/>
<c:set var="var2" scope="view" value="${ID2}" />
I tried in <c:if test=${var1 == var2}>
and eq also
above condition is not working. Both are same object. How to check?
View Replies
View Related
Apr 8, 2014
New to java/programming and i cant understand why the pen variable does not display the the correct value ... For example for input 1 ; 2 ; 3 ; 4 both variables will display 10 and i dont understand why pen does not have the value 6 .
import acm.program.*;
public class Chap4_ex12 extends ConsoleProgram {
public void run () {
int pen = 0;
int r = 1;
int sum = 0;
while (r !=SANTINEL) {
r = readInt(" ? ");
pen=sum ;
[code].....
View Replies
View Related
Jan 21, 2015
Variables defined in interface are public static and final so I was thinking that we should not be able to override the variables in a class thats implementing the interface. But when I am compiling the below class, it compiles fine and gives the correct values. but when I did disp.abhi = 35; it gives a compile error (cannot override final variable)
interface display{
int abhi = 10;
void displayName();
[code]....
View Replies
View Related
Jun 24, 2014
This is my first time working with C++ and I have put together this program and came up with two errors and I am unsure what it is wanting me to do. The errors I got are:
1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(30): error C2064: term does not evaluate to a function taking 1 arguments
1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(38): error C2064: term does not evaluate to a function taking 1 arguments
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//initialize arrays
int incr10(int* numa,int* numb);
[Code] ....
View Replies
View Related
Apr 13, 2014
I am new in this programming language, java. I have a problem after I set my path ";C:Program Files (x86)Javajdk1.7.0_51in". I made a simple program but an error occurred. Here's the screenshot.....
Attached image(s)
View Replies
View Related
Sep 23, 2014
I continuously get an error for lines 34, 36, and 37 saying that the variables may not have been initialized.
import java.util.Scanner;
import java.util.Random;
public class MathTutor {
public static void main(String[] args) {
Random r = new Random ();
Scanner input = new Scanner (System.in);
/*int min=1;
int max=10;*/
int num1,num2,operation;
int n1= r.nextInt((9+1)+1);
int n2= r.nextInt((9+1)+1);
operation= r.nextInt(3);
int correctAnswer;
int userAnswer;
[code]....
View Replies
View Related
Aug 30, 2014
Class 1:
view sourceprint?
1 public class one {
2 public static void main(String[] args){
3 int num = 0;
4 System.out.println(num);
5 two.change(num);
[Code] .....
This should print 0, then 1, but it prints 0, then 0. How do I make it print 0 then 1 with the same format?
View Replies
View Related
Apr 9, 2015
Let's say within a class I create a method that takes care of creating a java swing layout with labels, buttons etc.. then attach an action listener (inner class) for each button to change a respective label text. All I would need is that the action listener method can access and modify the label as needed.
Have read about static, protected, private, getters and setters but honestly bit confused about which structure should be adopted as a best practice. Global static protected variables for the labels along with private inner classes implementing ActionListeners believe will do the trick and will be able to access the labels but not convinced this is good practice.
View Replies
View Related
Jan 11, 2014
you can also refer this link Local variables in java?Local variables in java?To meet temporary requirements of the programmers some times..we have to create variables inside method or bock or constructor such type of variables are called as Local Variables.
----> Local variables also known as stack variables or automatic variables or temporary variables
----> Local variables will be stored inside Stack.
-----> The local variables will be created while executing the block
in which we declared it and destroyed once the block completed. Hence the scope of local variables is exactly same as the block in which we declared it.
package com.javatask.in;
class A{
public static void main(String args[]){
int i=0; // Local variable
[code]....
View Replies
View Related
Apr 13, 2014
Is there any way to save variables while I'm using applet as single runnable .jar file?
For example if I start app first time some variable has value of 100. While using app it changes to 200. After closing app it disapear and next run gives me 100 again instead of 200. Is there any way to save that 200?
View Replies
View Related
Oct 4, 2014
I will like to add to the questions about constructors and its this. I have a class A with a constructor, i have a class B which initialize the constructor. also i have a class C which needs a variable in class A but doesn't need to initialize the constructor of A. the question how do i access the variable of class A without initializing the constructor.
View Replies
View Related
Aug 15, 2014
Does a variable have public access modifier? if we can use it within the class and outside of the class then can i access a public variable as follows??
class mo
{
void display() {
public int a=9;
System.out.println(a);
}
public static void main(String[] args) {
mo m=new mo();
m.display();
}
}
ERROR:
It shows 6 errors :-O.
Error 1. illegal start of the expression
2. class,interface, or enum expected
View Replies
View Related
Apr 11, 2014
I'm having some trouble figuring out how to change the value of a variable that is passed into a class. I've tried just changing the variable, I've tried changing it by using the this.variable command, and I've even tried calling the setter class from within the class to change it, but it's still not working right. The first class is the one with Main in it and I just feed it some dummy data:
public class ExamConverter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Age testAge = new Age();
[Code] .....
This is the other class to calculate the age the way we do it on psych tests - it might not be mathematically accurate, but it's what all the tables and such for raw to scaled score conversion are based on, so the math needs to be the same as opposed to "accurate" because of some months having 30 or 31 days, etc.
public class Age {
//==================Properties==================
// Variables for the test date and client date of birth
private int TestMonth;
private int TestDay;
private int TestYear;
private int ClientMonth;
private int ClientDay;
private int ClientYear;
[Code] ......
Based on this dummy data, the output is:
Test: 5/4/2014
DOB: 5/5/1971
Age Years: 43 Months: 0 Days: 0
However, it should be:
Test: 5/4/2014
DOB: 5/5/1971
Age Years: 42 Months: 11 Days: 29
View Replies
View Related