Question

In: Computer Science

Write the following methods in a Java project: a) A Java method to determine and return...

Write the following methods in a Java project:

  1. a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters.

  2. 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.

  3. c) A Java method to determine and return an appropriate value indicating if a number K is even, where K is received as a parameter.

  4. d) A Java method to determine and return an appropriate value indicating if a word is present in a file, where the filename and word is received as parameters.

  5. e) Test the above methods from the main method.

Solutions

Expert Solution

import java.util.*;
import java.io.*;

class MedhodsDemo
{
Scanner input=new Scanner(System.in);

int sumNumbers(int a,int b,int c)
{
return (a+b+c);
}

int highestNumber(int N)
{
int number=0,highest=0;
for(int i=1;i<=N;i++)
{
System.out.print("Enter number "+i+" : ");
number=input.nextInt();
if(i==1)
{
highest=number;
}
if(highest<number)
{
highest=number;
}
}
return highest;
}

String checkEven(int number)
{
String ans="";
if(number%2==0)
{
ans=number +" is an even number.";
}
else
{
ans=number +" is not an even number.";
}
return ans;
}

String searchWordFromFile(String searchWord,String fileName)
{
int count=0;
try
{
String words[]=null;
String s;
File f1=new File(fileName);
FileReader fr=new FileReader(fileName);
BufferedReader br=new BufferedReader(fr);
if(!(f1.exists()))
{
System.out.println("File is not found!!");
System.exit(0);
}
while((s=br.readLine())!=null)
{
words=s.split(" ");
for(String w:words)
{
if(w.equals(searchWord))
{
count++;
}
}
}
br.close();
fr.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
return "The word is "+count+" times present.";
}
}

class Main
{
public static void main(String args[])
{
MedhodsDemo md=new MedhodsDemo();

Scanner input=new Scanner(System.in);
int no1,no2,no3,N,number;
System.out.println("\nMethod : 1\n");
System.out.print("Enter number 1 : ");
no1=input.nextInt();
System.out.print("Enter number 2 : ");
no2=input.nextInt();
System.out.print("Enter number 3 : ");
no3=input.nextInt();
System.out.println("Sum of three numbers : "+md.sumNumbers(no1,no2,no3));

System.out.println("\nMethod : 2\n");
System.out.print("Enter number of Integers : ");
N=input.nextInt();
System.out.println("Highest of N integers : "+md.highestNumber(N));

System.out.println("\nMethod : 3\n");
System.out.print("Enter a number : ");
number=input.nextInt();
System.out.println(md.checkEven(number));

System.out.println("\nMethod : 4\n");
System.out.print("Enter a word : ");
String searchWord=input.next();
System.out.print("Enter a file name with an extension : ");
String fileName=input.next();
System.out.println(md.searchWordFromFile(searchWord,fileName));

}
}


Related Solutions

In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
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.
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
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...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true...
java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true if the string parameter is a palindrome. This is the output example: Lets check if a string is a palindrom Please give me a string: abc No, its not a Palindrom Lets check if a string is a palindrom Please give me a string: noon Yes, its a Palindrom
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
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;...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT