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.
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
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...
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line...
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line arguments and calls the (add) method if the first parameter is a "+" calls the (subtract) method if the first parameter is a "-" calls the (doubled) method if the first parameter is a "&" add should add the 2 numbers and print out the result. subtract should subtract the 2 numbers and print out the results. Double should add the number to itself...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT