I am wrong. The method String valueOf(int i ).. I mensioned is to convert number to string stead of words.
tux4life commented: Good approach, I'd also have done this. :) +8
I am wrong. The method String valueOf(int i ).. I mensioned is to convert number to string stead of words.
Please make comments on my remarks on the following example I made of anonymous inner class.
The first anonymous inner class inheritants the abstract adapter class MouseAdapter while the second anonymous inner class extends the abstract adapter class WindowAdapter. Be noticed that not only the two inner classes have no names but also the two instances created by the inner classes have no references.
The advantage of this way to write code is simple and concise. For example, when you have to implement only one method of a interface, e.g. the mouseClicked() of the MouseListener,the rest methods of the corresponding interface will not bother you. If you implement an interface you have to implement all its methods.
The disadvantage is there are no references(name, status) to follow anywhere. Thus in rest code (e.g. outside of the containing class) there is no way to refer to them.
import javax.swing.*;
import java.awt.event.*;
public class InnerEx8 extends JFrame{
int x,y;
public InnerEx8(){
x=0;
y=0;
// register the mouseListener
addMouseListener (new MouseAdapter(){// the first anonymous inner class
public void mouseClicked(MouseEvent e) { //implement the mouseClicked method
System.out.println("x= " + e.getX() + ", y= " + e.getY()); /* On DOS window print the coordinate of the location clicked */
}
});
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
InnerEx8 ie= new InnerEx8();
/* register listener for windowClosing event */
ie.addWindowListener( new WindowAdapter(){ /* the second anonymous inner class for windowClosing event */
public void windowClosing(WindowEvent event){ /* implement the …
the conversion
Use the static method of class String:
String valueOf(int i )/String valueOf(double d)/String valueOf(float f)
in java.lang to convert number to words
For example, to get the factorial of n where n is in int. The result is the final value of fac.
long fac=1;
int i=1;
while ( i<=n ) { // the condition for execution of the loop
fac *= i; // the previous factorial (factorial of i-1)is multiplied by the current i
i++; // i has one increment
}
long fac=1;
int i=1;
do{
fac *= i; // the previous factorial (factorial of i-1)is multiplied by the current i
i++; // i has one increment
}while ( i<=n ) // the condition for execution of the loop
long fac=1;
for (int i=1; i<=n; i++) // the control variable i is initiated with 1
fac = fac*i; // the previous factorial (factorial of i-1)is multiplied by the current i
also see http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html
It's (the term override) perfectly valid
If the void print() method does not exist in the class Foo one can not override it in the sub-class Inner anymore. In other word, in the sub-class the void print() method is newly created, if its super class has no such a method. Therefore, if the void print() method is removed in the the super class Foo the term @Override should be deleted, implying the void print() method is newly created.
Based on your description I would assume that the method drawRect(int x0,int y0,int width, int length) has a time complxity O(n) since the plot shows
"an increasing linear trend at first".
The method must limit the width and length, that is, if either the width or the length reaches the maximum value (upper limit) it will remain unchanged. That's why
"the plot finally remains constant...".
The plot shows an real observation.
" then decrease rapidly"
may be due to some optimization of the operation when it is feasible.
what does this fucntion do. why do i need it if it is empty
You may insert some code into the method void print(){...}. If there is no such a method,
the line 23 @Override
should be deleted.
why do i need to use inheritance here?
In the class InnerEx7 foo2 (the instance of the class Inner) is created as an instance of Foo's subclass. If the class Inner does not inherit Foo, in the class InnerEx7 there is no way to get an instance of Inner since the class Inner is not recognined outside of the class Foo.
lines 44 and 45 are redundant.
it there a quick way to take the numbers back out
click the "Toggle Plain Text" above your code before copy it.
I have questions for you.
(1) Where is the definition of the class Console? As fas as I know it is a defined class in the package: java.io
(2) For line 15: in the class Graphics there is a member method drawRect(int x, int y, int width, int length), but in your code the handle is Console's instance. Where is the definition of its method?
(1) A comma is missing at the end of line 22
(2) input validation also includes other invalid cases, such as NumberFormatException, negative values for investment or number of years.
If both files under the same folder,
(1)remove line 3 in the file Main.java, i.e. remove "package deposit_withdraw;"
(2) in lines 31 and 34 in Main.java,
the method getBalance should be written as
myAccount.getBalance();
churva_churva , if the attribute
private int count;
in the definition of the class EmployeeNames represents a class-wide information, showing how many Employees in the list, it should be static.
Because there is no handle for the member method dispose(), hence there is no idea about which frame is to be disposed. Try:
frame.dispose();
(1) in the selection structure of "if else" you should place the "Beep-Beep" case (if (number%3==0 && number%5==0)...) as the first selection, then "else if (number%3==0 || number%5==0)" for the case "Beep" because the condition for "Beep-Beep" always meets the condition for "Beep".
if (number%3==0 && number%5==0) //the "Beep-Beep" case should be placed as the first selection since its condition always meets the condition for the next selection.
System.out.println("Beep-Beep");
else if (number%3==0 || number%5==0) // the second selection , i.e. the "Beep" case
System.out.println("Beep");
(2) For the above two cases you have no need to print up the numbers from 0 up to the limit. Only in the third case each number from 1 up to limit has to be printed.
(3) therefore, the case where each number from 1 up to limit has to be printed should be your third selection:
else { // the third selection/case
int count = 0;
while(count<=number)
System.out.println(count ++);
}
May I suggest you use the selection structure where the final selection will be associated with while loop to print a series of number up to the limit.
(1)The cases "Beep" and "Beep-Beep" should no be placed withing the while loop since they are each individual case respectively which should be done once, i.e. just print Beep or Beep-Beep.
Hence, they should be associated with the selection structure, such as
if (number%3==0 && number%5==0)
System.out.println("Beep-Beep");
else if (number%3==0 || number%5==0)
System.out.println("Beep");
(2) be noticed that the case : if (number%3==0 && number%5==0) should be the first case to be checked. (Why?)
(3) The case, which neither-("Beep") nor ("Beep-Beep"), should be the third selection in the if else structure. In the third selection you may use while loop to print out a series of count number. Therefore the third selection could be the while loop:
else while(count<=number)
System.out.println(count ++);
I guess nothing wrong with your applet code. At this stage please read some tutorial to find out how to run an applet program.
To run an applet, you need a Web page that includes it. A Web page is a text file that can be displayed by a Web browser.
(1)Create a String object, for instance s (e.g., String s="";) to accommodate the information obtained from your for loop:
for(int a=0 ; a<values.length ; a++){
System.out.println("[Firstname Length: "+values[0].length()+" ]"
+"[Lastname Length: "+values[1].length()+" ]"
+"[Employee IDnumber: "+(i+1)+" ]"+" = "+ staff);
a++;
}
(2)Create a BufferedWriter bw in the following way:
BufferedWriter bw = new BufferedWriter(new FileWriter("a .csv"));
(3)use the member method write(String s) of bw to write the information into the file "a .csv";
bw.write(s);
(4) close the BufferedWriter
bw.close();
You should use a browser or the command: appletviewer in DOS window to open a html file where the applet code, i.e. the file of the applet code is in the same directory/folder, is available:
<html><applet code="####.class" width=400 height=500></applet></html>
There is nothing to do with main method.
If the number is divisible by 3 or by 5
if (number%3==0 || number%5==0)
If the number is divisble by 3 and 5
if (number%3==0 && number%5==0)
The following errors should be corrected.
The code line 19 should be:
years = Integer.parseInt(yearsInvest);
The code line 20 should be:
while(yearsloop < years)
The code line 22 should be:
balance = investment * Math.pow(1.0 +rate/100, yearsloop);
The code line 23 should be:
JOptionPane.showConfirmDialog(null,
The code line 27 should be:
yearsloop = yearsloop + 1;
The output of the values in the JOptionPane.showConfirmDialog windonw shoud be formated.
(1)You have to assign the following variables an initial value, such as 0 for init or 0.0 for double in lines 10,11, and 12:
double withholding= 0.0;
double netPay= 0.0;
int withholding1= 0;
to pass the compiling.
(2) You have to delete the pair of curly brackets at the line 28 and the line 40
kvass's way is feasible.
Check the input digits' string to see if its length is 5 nor not. If not then ask client to re-enter a 5 digits' number.
There is no way to restrict number of characters a user types in at the console during typing.
If the digit number is understood as int data, then
class String has static String valueOf(int i) method which converts the int data into String.
jwmollman,
Can you modify your code by the following 2 changes?
(1) in line 34, replace intTriangleHeight with intIndex1 so that the line of code will be:
for (int intIndex2 = 0; intIndex2 <= intIndex1 ; intIndex2 += 1) // control the number of symbles for each row
(2) replace the code from line 35 and 37 with
System.out.print("S"); // print certain number symbles for each row
System.out.println(); // go to new line after completing printing for each row
(3) delete line 31
You may have to make more changes to accomplish the task (to adjust the number of rows) although the triangle is shown.
Good luck.
jackcfj0129,
(1) since you have established the correct method for calculating the monthly payment (public static double monthlyPayment (double aI, double loanAmt,int years )) can you write the code inside the main(String args[]) method to print out the information for the specific case as your professor indicated : loan for 5 years and the Loan Amount of 10,000? You may use a for loop to do 25 loops. The annual interests rate goes up with each increment of 0.125. The output is shown in the following image
(2) transform the main(String args[]) method into a static method for the future new main method to call:
public static void display(int y, double LoanAmount)
(3) Finally write a new main method where the client is asked to type in the information :
(a) loanAmount
(b) how many years for the loan
so that you may call the method: public static void display(int y, double LoanAmount)
to print out necessary information accordingly.
The formula for Monthly Payment is incorrect.
It should be:
Monthly payment = ((Loan Amount*Monthly Interest)/(1-(1+monthly interest)^(-number of years*12))
The method to calculate the Monthly Payment should thus be:
public static double monthlyPayment (double aI, double loanAmt,int years ){
double mp;
double mi = aI/1200;
mp = loanAmt * mi / (1 - Math.pow((1+mi), -years * 12));
return mp;
}
To call System.out.print() and use nested loops as well.
For present question the teacher of mental arithmetic may tell students count in one's head as follows.
There are 499 expressions.
1 + 999 = 1000
2 + 998 =1000
…
499 + 501 =1000
(only 500 is missing)
All expressions have the same outcome of 1000. So the outcome of adding all the whole numbers from 1 to 999 is 1000×499 + 500 (499,500).
The above understanding by count in one’s head can be explained by the following for loop
int sum = 0;
for (int i=1; i<=499; i++)
sum += i+ (1000-i) ; //which is equivalent to sum += 1000;
// only the number of 500 is missing
The above loop thus could be simplified by an expression:
sum = 499*1000 // do the accumulation of 1000 499 times
Therefore, the code for the following for loop:
int sum =0;
for (i=1;i<=999;i++)
sum += i;
could be simplified by an expression:
sum = 499*1000+500;
where the final sum (499,500) is the consequence of adding all the whole numbers from 1 to 999
Of course, we have to follow computer professor’s instruction to obtain the result of adding all numbers from 1 to 999 by a loop rather than an expression.
The JOptionPane's showMessageDialog is indeed shown, but it is covered by the DOS window. After you move the DOS window away you will be able to see the JOpationPane's showMessageDialog. This is my observation.
For the variable date should we define as String?
String date = "01/01/2010"; // To hold the date
The code in line 33 would be:
date = keyboard.nextLine(); // To receive the date as a String
the local variable intTotal may not have been initialized
The code in line 8 should be:
int intTotal=0; //so that the intTotal is initialized.
We can try a while or a do while loop. How would I go about it that way?
Define the condition for the while loop, as Jon.kiparsky discussed earlier, as follows
while(intCounter <=1000) {
...
}
You may add the rest of the code before, within the loop body, and after the loop.
(1) The code in the line 12 is not correct. The code in line 12 could be:
intTotal = intTotal + intCounter;
or
intTotal += intCounter;
(2) The code in line 13 should be moved/inserted into the place between the line 14 and 15.
(3) The initial value for the control parameter intCounter in the for loop could be 1 rathern than 0
jwmollman , in the future can you try to use while or do while loop to accomplish the task? or use a recursive/iterative method to do the job? We have various ways to go.
The difinition of the two methods from the line 15 to line 20 has some problem.
The line 16 should be:
return count== EmpYee.length;
The line 19 should be:
return count==0;
The following code shows an idea on how to have only the figures after the decimal point are printed after the integer part is removed. Please rewording my comments if it is necessary.
public class Prinf{
static double decimal(double d){ //a method to return the value after the decimal point
int dd = (int)d; // cast into the int data type so that only the integer part is stored in dd
d -=dd; // the value after the decimal point is finally left after the original value of the argument d minus the integer part dd.
return d; // return the residual value, i.e. the value after the decimal point
}
public static void main(String args[]){
double d = 6.328941; // for example, we have a double variable of a value with 6 figures after the decimal point.
System.out.printf("%8.6f\n",decimal(d)); // The 6 figures after the decimal point are printed after the integer part is removed.
}
}
The following code could be helpful.
public class Prinf{
static double decimal(double d){
int dd = (int)d;
d -=dd;
return d;
}
public static void main(String args[]){
double d = 6.328941;
System.out.printf("%8.6f\n",decimal(d));
}
}
output:
0.328941
Or simply put the header as:
import java.util.*;
acash229, If your intention is that the time when first time clicking the middle button is stored in long variable start which remains unchanged all the time, you need to declarare it as a global static variable like n.
static int n=0;
static long start =0;
Then should we replace the line 52 with
if (n==0)
start= System.currentTimeMillis(); // get the starting milli-seconds
so that the variable start remains unchanged after the starting milliseconds is stored in it?
In this way, the time gap grows and fast.
in lines 7,10, 13,
the methods nextInt(), nextFloat() are called. the () is missing. The code should be:
double inInt = nix.nextFloat();
...
int inYear = nix.nextInt();
...
int inLoan = nix.nextInt();
You are using the static method of pow of the Math class.
If your fomula is correct, the code for the calculation in line 16 should be:
double mPay = ( inLoan*inInt ) / ( 1 - ( 1 / Math.pow( ( 1+inInt ),inYear*12 ) ) );
For example, you want to get 2.0^4.0, then the result d in the type of double is:
double a=2.0, b=4.0;
double d = Math.pow(a,b);
int line 62, the denominator should be 1000.0:
elapsedTimesec1 =(double)(elapsedTimeSec - start)/1000.0;
also see:
http://www.daniweb.com/forums/thread310983.html
Should replace
line 14: int year = scan.nextInt(); with the following code:
int year; // declaration of the integer variable year
I have modified your code between line 41 and 62 as follows:
if (source == theMainButton) {
resizeComponent(this);
setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
long start = System.currentTimeMillis(); // get the starting milli-seconds
n++;
if(n>=8){
JOptionPane.showMessageDialog(null, "You Clicked it " +n);
long elapsedTimeSec = System.currentTimeMillis(); // get the stoping milli-seconds,
elapsedTimesec1 =(double)(elapsedTimeSec - start)/1000.0; // calculate the lasting time in seconds.
JOptionPane.showMessageDialog(null, "Took you: " + elapsedTimesec1 + " seconds."); // showing the result.
}
hope the changes are helpful.
The errors I have found are indicated as follows:
(1) line 10 is a header declaration (import java.util.*;", which should be place in "the heading part", i.e. the first line of the code.
(2) line 13:
The class name is Scanner rather than scanner;
(3) lines 25,27:
the method println(...) are missing handles. Both should be:
System.out.println(...);
(4) line 16 the variable year has not been delrared yet. The line 16 should be:
int year = scan.nextInt();
(5) In line 20 the boolean variable's name (leapyear ) is not consistent with the later name (isLeapYear) in line 23. Both should be identical.
You did not difine the following classes:
gradeTypePabel,namePanelname, testPanel panel , and resultPanel
Call stop() method to kiill the thread.
import java.lang.Runnable;
public class T implements Runnable{
Thread timer = null;
public void start() {
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void stop() {
timer = null;
}
public void run( ) {
while (timer != null) {
try {Thread.sleep(100);} catch (InterruptedException e) {}
// do something
}
timer = null;
}
}
If I understand correctly you want to do some java program for cell phone. You need a J2ME development environment so that you may develop some java application, resulted in a file.jar (an executable jar file) ready for cell phone.
Your computer should have JDK 1.6 environment. Then
download the Micro Edition Software Development Kit 3.0
The methods of an inner/nested class may also directly access the variables of its top-level class. So in some case one may declare a class entirely within the body of another class
nydas, Thank you for your information. After checking with internet, one may come to the conclusion: Indic language is a very special Indian language which can not be represented by UNICODE. Perhaps it works by the combination of ISCII (Indian Standard Code for Information Interchange) and Unicode.
See MUKRI: Indic computing in Java made easy!
The primitive data type char in java is represented by Unicode. Therefore, the reverse() method of StringBuffer works with Unicode, i.e., works with many kinds of Characters, such Chinese, Japanese, Russian,.....