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

Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
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:...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of size three as the parameter, that sorts the values of the array into ascending order. Ex: If the array contains [5, 2, 7], after the call Ascend3(int[] vals), the array will now hold [2, 5, 7]. Hints: Return type should be void. One approach puts the three values into an array, then sorts the array. We won't be describing that approach here. Instead, we'll...
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...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.
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...
Write a method (in Java) that will return output as stated below: toThePowerOf(int): Applies exponentiation to...
Write a method (in Java) that will return output as stated below: toThePowerOf(int): Applies exponentiation to the existing PolyTerm. For example, 2.4x^3 to the power of 3 should return 13.824x^9 while 2.4x^3 to the power of -3 should return 0.0723x^-9 Respective JUnit test for this question: public void testToThePowerOf() {        assertEquals(1, t1.toThePowerOf(3).coefficient, 0.0001);        assertEquals(3, t1.toThePowerOf(3).exponent);        assertEquals(13.824, t2.toThePowerOf(3).coefficient, 0.0001);        assertEquals(9, t2.toThePowerOf(3).exponent);        assertEquals(0.0723, t2.toThePowerOf(-3).coefficient, 0.0001);        assertEquals(-9, t2.toThePowerOf(-3).exponent);        assertEquals(-0.2962,...
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT