4.3 Write a Method Called Sum100 That Returns the Sum of The

4.3 Write a Method Called Sum100 That Returns the Sum of The

<p> 1 ______Sum_100 4.3 Write a method called sum100 that returns the sum of the integers from 1 to 100, inclusive. import cs1.Keyboard; public class Sum_100 { public static void main (String[] args) { int total=0; System.out.println ( "The sum of numbers 1 to 100 is: " + sum100(total)); }</p><p> public static int sum100 (int total) { for (int i=1; i<=100; i++) { total = total + i; } return total; } }</p><p>______Sum_between 4.5 Write a method called sumRange that accepts two integer parameters that represent a range. Issue an error message and return zero if the second paramenter is less than the first. Otherwise, the method should return the sum os the integers in that range (inclusive). import cs1.Keyboard; public class Sum_between { public static void main (String[] args) { int n1, n2; System.out.print ("Enter a number: \t"); n1=Keyboard.readInt(); System.out.print ("Enter a second number: \t"); n2=Keyboard.readInt();</p><p> if (n1>=n2) { System.out.print ("\nError: a second number must be larger\n\n\"); }</p><p>System.out.println ( "\nThe sum of numbers from "+n1+" to "+n2+" is: "+sumRange(n1, n2)); }</p><p>//------the method that does the actual calculations: public static int sumRange (int n1, int n2) { int total=0;</p><p> for (int i=n1; i<=n2; i++) { total=total+i; } return total; } } 2 ______Larger_6 4.6 Write a method called larger to find if the first number is larger than the second one? import cs1.Keyboard; public class Larger_6 { public static void main(String[] args) { double n1=0, n2=0; System.out.print ("Enter a number: \t"); n1=Keyboard.readDouble(); System.out.print ("Enter a second number: \t"); n2=Keyboard.readDouble(); System.out.println ( "\nIs first number larger than the second one? ---- " + larger(n1,n2)); }</p><p> public static boolean larger(double n1, double n2) { if (n1 > n2) { return true; } return false; } }</p><p>______CountA_7 4.7 Count a total number of 'a' or 'A' in any word or sentence import cs1.Keyboard; public class CountA_7 { public static void main (String[] args) { String userinput = ""; System.out.println ("Enter any word/sentence : "); userinput = Keyboard.readString(); System.out.println ("\nTotal number of A/a: " + countA(userinput)); }</p><p> public static int countA (String words) //can be 'userinput' { int totalA=0; for (int i=0; i<words.length(); i++) { if (words.charAt(i) == 'A' || words.charAt(i) == 'a') { totalA = totalA + 1; } } return totalA; } } 3 ______EvenlyDivisible_8 4.8 Check if the first number is evenly divisible by the second number. import cs1.Keyboard; public class EvenlyDivisible_8 { public static void main (String[] args) { System.out.print ("Enter a number: \t"); int num1 = Keyboard.readInt(); System.out.print ("Enter a second number: \t"); int num2 = Keyboard.readInt(); System.out.println ("\nChecking if the numbers are evenly divisible:\n"); System.out.println ("Exit status is " + evenlyDivisible(num1, num2)); } public static boolean evenlyDivisible (int n1, int n2) { boolean exit_status = false; if (n1%n2 == 0) { exit_status=true; } return exit_status; /*------or just: return (n1%n2 == 0); ------*/ } }</p><p>______Concat_12 4.12 Write a methos called multiConcat that takes a String and an integer as parameters. Return a String that consists of of the string parameter concatenated with itself count times, where count is the integer parameter. For ex, if the parameter is "test", the return value is "testtest". import cs1.Keyboard; public class Concat_12 { public static void main(String[] args) { String inputstring=""; int RF=0; //stands for Repeat Factor System.out.print ("Enter a number: "); RF=Keyboard.readInt(); System.out.println ( "\nEnter a string and script will concatinate the string to itself "); System.out.println ( "as many times as the number above:"); inputstring=Keyboard.readString(); System.out.println(multiConcat(inputstring, RF)); }</p><p>//------multiConcat method with 2 args</p><p> public static String multiConcat(String inputstring, int RF) { String temp=""; for (int i=0; i<RF; i++) { temp=temp.concat(inputstring); } return temp; } 4 } ______Concat_13 4.13 Overload the multiConcat method from Ex. 4.12 import cs1.Keyboard; public class Concat_13 { public static void main(String[] args) { String inputstring=""; System.out.println ( "\nEnter a string and script will concatinate the string to itself 2 times:"); inputstring=Keyboard.readString(); System.out.println(multiConcat(inputstring)); }</p><p>//------overloaded multiConcat method with 1 arg public static String multiConcat(String inputstring) { inputstring=inputstring.concat(inputstring); return inputstring; } } ______Alpha_14 4.14 Write a methos called isAlpha that accepts a character parameter and returns true if that character is either an uppercase or lowercase alphabetic letter. import cs1.Keyboard; public class Alpha_14 { public static void main(String[] args) { char inputchar=' '; System.out.print( "\nEnter a character and script will determine if it's an alphabetic: "); inputchar=Keyboard.readChar(); System.out.println(isAlpha(inputchar)); } //---call the Java's method isLetter, which is under the Character class public static boolean isAlpha(char inputchar) { return (Character.isLetter(inputchar)); } } ______Triangle_17 4.17 Enter 3 numbers representing the three sides of a triangle and script will determine if the triangle is isosceles import cs1.Keyboard; public class Triangle_17 { public static void main(String[] args) { int n1=0, n2=0, n3=0; System.out.println ( "Enter 3 numbers representing the three sides of a triangle, "); System.out.println ("and I will check if the triangle is isoceles \n"); System.out.print ("side1: "); n1=Keyboard.readInt(); System.out.print ("side2: "); n2=Keyboard.readInt(); System.out.print ("side3: "); n3=Keyboard.readInt(); System.out.println(isIsoceles(n1, n2, n3)); } public static boolean isIsoceles (int n1, int n2, int n3) { return (n1==n2 && n1==n3); 5 } }</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us