1. You have a file called \"salaries.txt\" that contains the salaries of all you
ID: 3569271 • Letter: 1
Question
1. You have a file called "salaries.txt" that contains the salaries of all your employees, where each salariey is on its own line and an int equivalent. Write a method that reads in each salary from file and returns, to the calling client, the largest salary. You must include exception handling for any exception that may occur.
2. Consider the following method, what is the value returned as a result of the call MethodC(6,2)?
public int MethodC(int x, int y)
{
if (x<y)
return x;
return MecthodC(x-1,y+1);
}
Explanation / Answer
Here you go :)
comment if you have any doubts
public int returnMax()
{
int ret=Integer.MIN_VALUE;
try{
Scanner sc=new Scanner(new FileReader("salaries.txt"));
String line="";
int max=Integer.MIN_VALUE;
while((line=sc.nextLine())!=null)
{
int sal=Integer.parseInt(line);
if(max<sal)max=sal;
}
ret=max;
sc.close();
}catch(IOException e){System.out.println(e.getMessage());}
return ret;
}
Q2) ans = 3
step1=>6>2 so calls MethodC(5,3)
step2=>5>3 so calls MethodsC(4,4)
step3=>4=4 so calls MethodsC(3,5)
step4=>3<5 so returns 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.