Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String txt1 = "The quick brown fox jumps over the lazy dog";
- //using charAt()
- for (int i = 0; i < txt1.length(); i++) {
- System.out.print(txt1.charAt(i) + ", ");
- }
- // using toCharArray()
- char[] txt1Char = txt1.toCharArray();
- for (char c : txt1Char) {
- System.out.print(c + "|");
- }
- //-----------------------
- char[] charArray1 = {'l','u','p','a','n','g',' '
- ,'h','i','n','i','r','a','n','g'};
- int[] vowelCounter = {0,0,0,0,0};
- int aCount = 0;
- int eCount = 0;
- int iCount = 0;
- int oCount = 0;
- int uCount = 0;
- for (char c : charArray1) {
- switch (c) {
- case 'a':
- aCount++;
- vowelCounter[0]++;
- break;
- case 'e':
- eCount++;
- vowelCounter[1]++;
- break;
- case 'i':
- iCount++;
- vowelCounter[2]++;
- break;
- case 'o':
- oCount++;
- vowelCounter[3]++;
- break;
- case 'u':
- uCount++;
- vowelCounter[4]++;
- break;
- }
- }
- System.out.println("Total a: " + aCount);
- System.out.println("Total e: " + eCount);
- System.out.println("Total i: " + iCount);
- System.out.println("Total o: " + vowelCounter[3]);
- System.out.println("Total u: " + vowelCounter[4]);
- //------------------------
- package week3training;
- public class Day15C {
- public static void main(String[] args) {
- Day15C callMe = new Day15C();
- double[] numberList = {9.8, 273.15, 299,42};
- System.out.println("Sum of numbers in array are: "
- + callMe.getSum1(numberList));
- // kelangan pa gamitan ng sout yung getSum Method para makita result
- System.out.println("using getSum2 method:");
- callMe.getSum2(numberList);
- String txt1 = "salitaan";
- System.out.println(txt1);
- callMe.changeText(txt1);
- System.out.println("After method: " + txt1);
- }
- public double getSum1 (double numArray[]) {
- double sum = 0;
- for (double d : numArray) {
- sum += d;
- }
- return sum;
- }
- //using Day12B as a reference
- public void getSum2 (double numArray[]) {
- double sum = 0;
- for (int i = 0; i < numArray.length; i++) {
- double number = numArray[i];
- sum += number;
- if (i < (numArray.length - 1)) {
- System.out.print(number + " + ");
- } else {
- System.out.print(number + " = ");
- }
- }
- System.out.println(sum);
- }
- public void changeText (String txtRef) {
- txtRef = "changed";
- }
- }
- //--------------------------------------------
- package week3training;
- public class Day15D {
- // no main method
- public void countVowels (String txtSentence) {
- int aCount = 0;
- int eCount = 0;
- int iCount = 0;
- int oCount = 0;
- int uCount = 0;
- char[] txt1Char = (txtSentence.toLowerCase()).toCharArray();
- for (char c : txt1Char) {
- switch (c) {
- case 'a':
- aCount++;
- break;
- case 'e':
- eCount++;
- break;
- case 'i':
- iCount++;
- break;
- case 'o':
- oCount++;
- break;
- case 'u':
- uCount++;
- break;
- }
- }
- System.out.println("Sentence: " + txtSentence);
- System.out.println("Total a: " + aCount);
- System.out.println("Total e: " + eCount);
- System.out.println("Total i: " + iCount);
- System.out.println("Total o: " + oCount);
- System.out.println("Total u: " + uCount);
- }
- public void capitalizeEachWord (String txtRef) {
- String[] words = txtRef.split(" ");
- String txtRefModded = "";
- for (String word : words) {
- if (!word.isEmpty()) {
- String firstLetter = word.substring(0,1).toUpperCase();
- String remains = word.substring(1).toLowerCase();
- txtRefModded += firstLetter + remains + " ";
- }
- }
- System.out.println(txtRefModded);
- }
- }
- //--------------------------------------------
- package week3training;
- public class Day15E {
- public static void main(String[] args) {
- Day15D callDay15D = new Day15D();
- String txtReference = "The quick brown fox jumps over the lazy dog.";
- callDay15D.countVowels(txtReference);
- callDay15D.capitalizeEachWord(txtReference);
- System.out.println("---");
- txtReference = "Who would know naught of art must learn, act, and then take his ease.";
- callDay15D.countVowels(txtReference);
- callDay15D.capitalizeEachWord(txtReference);
- System.out.println("***");
- txtReference = "Lake, tree, pine cone, cool brown soil.";
- callDay15D.countVowels(txtReference);
- callDay15D.capitalizeEachWord(txtReference);
- System.out.println("~~~");
- txtReference = "Fat pet pig got drunk, took walk.";
- callDay15D.countVowels(txtReference);
- callDay15D.capitalizeEachWord(txtReference);
- System.out.println("###");
- txtReference = "A hidden litter of little kittens.";
- callDay15D.countVowels(txtReference);
- callDay15D.capitalizeEachWord(txtReference);
- System.out.println("----------------");
- String txt2 = "magical";
- System.out.println(txt2.substring(1,4));
- // 1st number = start
- // 2nd number = index - 1 where to stop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment