tong1 22 Posting Whiz

The following code demonstrates how to analyze a keyboard input in terms of tokens. The client may input no more than 100 words with the final string “end” (excluded). These words are simultaneously stored in the String array s, through which one may do search for a specific word.

import java.util.*;
public class Tokenizer0 {	
	public static void main(String args[]){
		Scanner asdf = new Scanner(System.in); // obtain an instance of Scanner
		String s []= new String[100]; // declare a string array with the size of 100 to accommodate the input words via asdf
		int counter = 0; // counter counts the number of input words
		
		while(asdf.hasNext()){ //returns true if asdf has next token
		s[counter++]=asdf.next(); // assign the next token to the string array
		if (s[counter-1].compareTo("end")==0) break; // check the input word. if the input is "end", then stop the while loop
		}
		System.out.println("Number of Words:" + (counter-1)); // print out the total number of input words
		for (int i=0; i<counter-1; i++) // print out the input words
		System.out.println(s[i]);
	}
}
kramerd commented: You are counting the number of lines, not words +0
tong1 22 Posting Whiz

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.

tong1 22 Posting Whiz

Your code is basically correct. Please use the tag [CODE] for your code.
I have made a small modification. I am not sure if there is a term called "accumulated interests" which is different from simple interests.

import java.lang.*;
public class simple {
public static void main (String[] args){
// this program calculates the simple interest.
int principal, noy;
double rate, si;
principal = 1000;
noy = 3;
rate = 2.5;
si = (principal * noy * rate) /1000;
double d = (Math.pow((1.0 + rate/100), (double)noy) - 1)*100;
System.out.printf (" The principal is %d \n number of years: %d, \nrate is: %f \n The calculated simple interest is:%f\n ", principal,noy,rate,si);
System.out.printf("Accumulated interests: %5.2f\n", d);  // using C_style to define the output of the  double
    }
}

output:
The principal is 1000
number of years: 3,
ate is: 2.500000
The calculated simple interest is:7.500000
Accumulated interests: 7.69

masijade commented: do not simply do other's homework. At the very least [i]explain[/i] the changes. Better, just tell them what to change. They don't learn anything this way. -2
tong1 22 Posting Whiz

Should we start with one bubble sort?
Can you explain why
1. the usefulness of the boolean flag "change" ?
2. why the upper limit of the embedded loop ( “ for(j=0;j<i;++j) ” )is i rather than n?

You may figure out the descending sort and other sorting methods, such as insertion and selection.
/* The following program is written to receive a size of an int array that 
  * assigned by random values varying between 0 to 255 [0-255].
  * Then the array will be sorted in ascending order by the bubble method.
  */ 
 import java.lang.*; 
 public class MainExample1 {
 static void bubble_sort(int a[]){
   int i,j,t, n=a.length;
   boolean change;
   for(i=n-1,change=true;  i>0 && change ;--i)
   {
     change=false;
     for(j=0;j<i;++j)
       if(a[j]<a[j+1])
       {
         t=a[j];
         a[j]=a[j+1];
         a[j+1]=t;
         change=true;
       }
   }
 }

 public static void main(String args[]){
    int n= Integer.parseInt(args[0]);
    int d[] = new int[n];
    for (int i=0; i<n; i++)
    d[i] = (int)(Math.random()*256);
    bubble_sort(d);
    for (int i=0; i<d.length;i++)
    System.out.print(d[i] + " ");
    System.out.println();
    }
 }