Question

In: Computer Science

Please answer with a fully detailed solution with examples. In Java, write a method to determine...

Please answer with a fully detailed solution with examples.

In Java, write a method to determine if a string contains only digit characters.
In Java, write a method on how to find each occurrence of a letter given a string.
In Java, given a list of numbers, write an algorithm to find all the triples and pairs of numbers in the list that equal exactly 13.
In Java, write a function to find the total number of each character in a given string.

Solutions

Expert Solution

Java code for above problems

import java.util.*;
class Main
{
   // testing main method
   public static void main(String args[])
   {
       System.out.println("Does \"123\" has all digit characters? "+allDigits("123"));
       System.out.println("Does \"12a3c\" has all digit characters? "+allDigits("12a3c"));
       System.out.println();
       countChars("alphabets");
       System.out.println();
       List<Integer> list=new ArrayList<Integer>();
       list.add(8);
       list.add(2);
       list.add(4);
       list.add(13);
       list.add(3);
       list.add(5);
       tripletsAndDoubles(list);
   }
   // method that returns true if given string has all characters as digits
   public static boolean allDigits(String str)
   {
       for(int i=0;i<str.length();i++)
       {
           char ch=str.charAt(i);
           if(!(ch>='0' && ch<='9'))
           {
               return false;
           }  
       }  
       return true;
   }
   // method that prints the occurences count of all the characters of given string
   public static void countChars(String str)
   {
       int [] count=new int[256];
       for(int i=0;i<str.length();i++)
       {
           count[str.charAt(i)]++;
       }
       for(int i=0;i<256;i++)
       {
           if(count[i]>0)
           {
               System.out.println((char)i+" ---> "+count[i]);
           }
       }
   }
   // method that prints the triplets and pairs of given list whose sum is 13
   public static void tripletsAndDoubles(List<Integer> list)
   {
       System.out.println("Triplets: ");
       printTriplets(list);
       System.out.println();
       System.out.println("Pairs: ");
       printPairs(list);
       System.out.println();
   }
   // method that prints the triplets whose sum is 13
   public static void printTriplets(List<Integer> list)
   {
       int len=list.size();
       for(int i=0;i<len;i++)
       {
           for(int j=i+1;j<len;j++)
           {
               for(int k=j+1;k<len;k++)
               {
                   if(list.get(i)+list.get(j)+list.get(k)==13)
                   {
                       System.out.println(list.get(i)+"+"+list.get(j)+"+"+list.get(k)+"=13");
                   }
               }
           }
       }
   }
   // method that prints the pairs whose sum is 13
   public static void printPairs(List<Integer> list)
   {
       int len=list.size();
       for(int i=0;i<len;i++)
       {
           for(int j=i+1;j<len;j++)
           {
               if(list.get(i)+list.get(j)==13)
               {
                   System.out.println(list.get(i)+"+"+list.get(j)+"=13");
               }
           }
       }
   }
}

Sample output

Note: Question 2 and 4 are same. So same will be repeated for both.

Mention in comments if any mistakes or errors are found. Thank you.


Related Solutions

Please answer with a fully detailed solution with examples. Difference between interpretive and compiler languages? What...
Please answer with a fully detailed solution with examples. Difference between interpretive and compiler languages? What are the four pillars of OOP and provide a simple example for each and code as well? What is the highest superclass in Java and why and what are the other ones and explain them as well. Describe the components involved in the processing of one web request.
Please answer ALL parts of the question fully with detailed explanations. #Q5 a)Looking at the rat’s...
Please answer ALL parts of the question fully with detailed explanations. #Q5 a)Looking at the rat’s heart, how does the mammalian heart differs from those of reptiles? And from those of fishes? How similar is it to a bird’s heart? b)What advantage has the mammalian heart over the reptilian heart? Use diagrams if necessary. c) Compare & contrast both the skeletal systems and digestive systems individually in pigs, cats, and sheep. d) pig vs cat : What is the length...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
please answer the question : Explain fully, with examples, what dollar cost averaging is. What will...
please answer the question : Explain fully, with examples, what dollar cost averaging is. What will happen (1) if the price of an investment trends down overtime; (2) trends up; (3) trends down then up; and (4) in real life? Use excel to model and graph the result.
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Polaris industries inc. 1. What is their competitive advantage? Detailed answer + examples please
Polaris industries inc. 1. What is their competitive advantage? Detailed answer + examples please
Please answer as detailed as possible and from outside examples too! In this paper, we study...
Please answer as detailed as possible and from outside examples too! In this paper, we study the case of a central public agency in a mature welfare state. The Swedish National Board of Student Aid (known as the CSN) manages the payment of financial aid to students for their living costs. CSN is divided into two administrative units: “Out” and “In”. “Out” includes those sections within the agency that manage the payment of financial aid to students. The “In” unit...
please answer the question correct: Explain fully, with examples, what dollar cost averaging is. What will...
please answer the question correct: Explain fully, with examples, what dollar cost averaging is. What will happen (1) if the price of an investment trends down overtime; (2) trends up; (3) trends down then up; and (4) in real life? Use excel to model and graph the result.
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT