****************6.0 Method******************************** ****************************************************** import java.util.*; class Main { public static Scanner in= new Scanner( System.in ); public static void main(String[] args) { int start, finish; int total; say( "Compute the sum of integers within a range."); start= ask( "What is the start of the range?" ); finish= ask( "What is the end of the range? " ); total= addRange( start, finish ); say( "The (inclusive) sum is", total ); } // Method to read input (after prompting). // public static int ask( String s ) { int result; System.out.print( s ); result= in.nextInt(); return result; } // Method to display a message. // public static void say( String s ) { System.out.println( s ); } // Method to display a message & a value. // public static void say( String s, int j ) { System.out.println( s + " " + j ); } // Method to add up a range. // public static int addRange( int first, int last ) { int result=0; for (int j=first; j<=last; ++j ) { // Add the next number to the total. result += j; } return result; } } ****************integer divisions******************************** ****************************************************** class Main { public static void main(String[] args) { //// FLOAT EXPRESSIONS say( " 5/3" ); say( "5.0/3.0 is " + 5.0/3.0 ); say( "5/3.0 is " + 5/3.0 ); say( "5.0/3 is " + 5.0/3 ); say( " 2/3" ); say( "2.0/3.0 is " + 2.0/3.0 ); say( "2/3.0 is " + 2/3.0 ); say( "2.0/3 is " + 2.0/3 ); say( " 1/3" ); say( "1.0/3.0 is " + 1.0/3.0 ); say( "1/3.0 is " + 1/3.0 ); say( "1.0/3 is " + 1.0/3 ); say( " 1/3 + 2/3" ); say( "1.0/3.0 + 2.0/3.0 is " + (1.0/3.0 + 2.0/3.0) ); //// INTEGER EXPRESSIONS: say( "\nNow, try this with integer division!" ); say( "5/3 is " + 5/3 ); say( "2/3 is " + 2/3 ); say( "1/3 + 2/3 is " + 1/3 + 2/3 ); say( "\nFractions only." ); say( (5%3)/3.0 ); say( 5.0%3 / 3 ); say( 5/3 + 5.0%3 / 3 ); //// EXAMPLE. Illustrates why you should never compare floats (or doubles). say( "\nNever compare floats (or doubles)." ); example(); } //// METHODS //// static void example() { say( " a= 5 / 3.;" ); say( " b= 1 + 2 /3.;" ); say( " // 5/3 should be the same as 1+2/3"); double a, b; a= 5 / 3. ; b= 1 + 2 /3. ; say( "a is ", a ); say( "b is ", b ); say( " if (a == b) // equal" ); say( " else // unequal" ); if (a == b) say( "Both are equal."); else say( "These values are unequal."); } //// I/O METHODS //// static void say( String s ) { // Display a String System.out.println( s ); } static void say( double x ) { // Display a double System.out.println( x ); } static void say( String s, double x ) { // Display string & value System.out.println( s +" "+ x ); } } ****************6.2******************************** ****************************************************** //// c6.2: Sum the digits (using a method.) import java.util.*; class Main { public static Scanner r= new Scanner( System.in ); // Main method. // public static void main(String[] args) { int number, total; double a; int lastDigit; do { a= ask( "\nEnter the next number: "); number= (int) a ; // Convert to integer. if (number ==0) break; if (number < 0) { say( "DON'T BE SO NEGATIVE!" ); continue; } total= sumDigits( number ); say( "Sum of the digits is: " + total ); lastDigit= number % 10; System.out.println( "Last digit is: " + lastDigit ); //// Check divisibility. if (lastDigit % 2 == 0) say( number + " is even" ); if (total % 3 == 0) say( "Divisible by 3." ); if (lastDigit == 0 || lastDigit == 5) say( "Divisible by 5."); if ( isFactor( 7, number )) say( "Divisible by 7." ); int j=11; if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; j=j+2; if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; j=j+2; // 23 if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; j=j+2; j=j+2; // 29 if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; if ( isFactor( j, number )) say( "Divisible by " + j ); j=j+2; j=j+2; j=j+2; // 37 if ( isFactor( j, number )) say( "Divisible by " + j ); if (number > 1000) say( "> 1000" ); } while( number != 0 ); // Exit loop if zero is entered. // say( "DONE" ); return; }// END OF main method. // // I/O methods. // public static void say( String s ) { System.out.println( s ); } public static double ask( String s ) { System.out.print( s ); return r.nextDouble( ); } // Return the sum of the digits. // public static int sumDigits( int n ) { int sum=0; while (n > 0) { sum += n % 10; // Last digit of Number sum = sum + n % 10; // Last digit of Number n /= 10; // Throw away last digit.\! n= n / 10; // Throw away last digit.\! } return sum; }// END OF sumDigits() method. // // Return true if f is a factor of n. // public static boolean isFactor( int f, int n ) { boolean result; if (n % f == 0) { result=true; } else{ result=false; } return result; }// END OF isFactor() method. // // Return true if f is a factor of n. // public static boolean isFactor1( int f, int n ) { return (n % f == 0); }// END OF isFactor() method. // // Return true if f is a factor of n. // public static boolean isFactor2( int f, int n ) { boolean result; result = (n % f == 0) ? true : false; return result; }// END OF isFactor() method. // // Return true if f is a factor of n. // public static boolean isFactor3( int f, int n ) { boolean result=false; if (n % f == 0) result=true; return result; }// END OF isFactor() method. // }// END OF class Main. // ****************6.2******************************** ****************************************************** //// c6.2: Sum the digits (using a method.) //// Zhilin Kuang import java.util.Scanner; class Main { // Main method. // public static void main(String[] args) { int sum; Scanner input = new Scanner (System.in); // ADD INPUT CODE HERE // System.out.print("Please enter a number: "); int n1= input.nextInt (); sum= sumDigits( n1 ); System.out.println( "The sum of the digits is: " + sum ); }// END OF main method. // // Return the sum of the digits. // public static int sumDigits( int n1) { int result=0, remainder=0; for ( int n = n1; n>0; n=n/10 ) remainder += n % 10 ; result += remainder; return result; //return remainder; }// END OF sumDigits() method. // }// END OF class Main. // ****************6.3******************************** ****************************************************** ////6.3: Palindrome & reverse ////Author: Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int n, N; System.out.print ("Enter an integer: "); n= input.nextInt (); System.out.print ("The reverse of this integer is: "); N=reverse (n); System.out.print (N); if (n==N) { System.out.println ( "\n"+ n + " is a palindrome."); } else { System.out.println ( "\n"+ n + " is NOT a palindrome."); } } //Deserted loop method /*public static void reverse (int n) { while (n!=0){ int remainder= n%10; System.out.print (remainder); n=n/10; }*/ //I replaced the void method with a returnValue method. public static int reverse (int n){ int result = 0; for (int a=n; a>0; a/=10){ result = (result*10) + (a % 10); } return result; } } ****************6.5******************************** ****************************************************** import java.util.Scanner; class Main { public static void main(String[] args) { System.out.print("Enter 3 different numbers:"); Scanner input = new Scanner (System.in); double n1,n2,n3,temp=0,temp2=0; n1= input.nextDouble(); n2= input.nextDouble(); n3= input.nextDouble(); if (n1n2 else { //n1>n2>n3 if (n2>n3){ n2=n2; temp=n3; n3=n1; n1=temp; } //n3>n1>n2 else if(n3>n1){ temp=n1; n3=n3; n1=n2; n2=temp; } //n1>n3>n2 else { temp=n1; temp2=n2; n2=n3; n1=temp2; n3=temp; } } System.out.println( n1 + "<" +n2 + "<" + n3); System.out.println( "The median of these 3 numbers is: " + n2); } } ****************5.12******************************** ****************************************************** //Excercise 5.12 Find the smallest n such that n^2 > 12000 //Author: Zhilin Kuang class Main { public static void main(String[] args) { int n=0; while (Math.pow(n,2) <=12000){ n++; } System.out.println ("The number is " + n); } } ****************5.13******************************** ****************************************************** //Excercise 5.13 Find the largest n such that n^3 < 12000 //Author: Zhilin Kuang class Main { public static void main(String[] args) { int n=12000; while (Math.pow(n,3) >12000){ n--; } System.out.println ("The number is " + n); } } ****************while loop******************************** ****************************************************** //// List all factors of a number and tell if it is a prime. //// CST 112: Zhilin import java.util.Scanner; class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, f, i; boolean prime; do{ /* I a scratching out all these codes below. System.out.print( "\nFactor Check \nEnter an interger: "); i=in.nextInt(); System.out.print( "Enter a potential factor: "); f=in.nextInt(); if ( isFactor(i, f) ) { System.out.println( f + " is a factor of " + i ); } else { System.out.println( f + " is NOT a factor of " + i ); }*/ // Code starts here. // // Read a number and list its factors. // System.out.print( "\nEnter the next number: "); n= in.nextInt(); System.out.print ("The factors of "+n+ " are: "); /*If number is prime, prompt message there's no factors that's not 1 and itslef.*/ if (prime (n)){ System.out.print (n+" does not have factors other than 1 and itself."); } //If number is not prime, loop to list factors// else{ f=n; while ( f>1) { f--; if (n%f == 0 && f!=1) { System.out.print (f + " "); } } } // Tell if the number is prime. // if ( prime(n) ) { System.out.println( "\n"+n +" is a prime number." ); } else { System.out.println( "\n"+n +" is NOT a prime number." ); } } while (n!=0); } // I commented out my isFactor method. It doesn't seem like I still need it. //// Return true iff the first argument ("factor") is a factor of the second number. /*public static boolean isFactor( int a, int b ) { boolean result; if (a % b == 0) { result=true; } else{ result=false; } return result; }*/ public static boolean prime ( int a ) { boolean result; int f=a, n=0; while ( f>1) { f--; if (a%f == 0) { n+=f; } } if (n>1||a==0) { result=false; } else{ result=true; } return result; } } ****************for loop******************************** ****************************************************** //// List all factors of a number and tell if it is a prime. //// CST 112: Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; boolean prime; do { // Read a number. // System.out.print( "\nEnter the next number: " ); n=in.nextInt(); // List factors. // if (findFactor (n)){ System.out.println ("are factors of " + n); } // Tell number is prime. // else { //Check for 0 first. // if (n==0) { System.out.println( n + " is NOT a prime number, all real numbers are factors of 0." ); } else { System.out.println( n + " is a prime number." ); } } } while (n!=0); } //// Return true iff the first argument ("factor") is a factor of the second number. public static boolean isFactor( int factor, int number ) { boolean result; if (number % factor == 0) { result=true; } else{ result=false; } return result; } // Loop to list the factors and check if number is prime. // public static boolean findFactor (int n) { boolean result=false; for (int j=n-1; j>1; j--){ if ( isFactor(j, n) ){ System.out.print( j + " " ); result=true; } } return result; } } ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ******************************************************