Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q.1 Ask user for first name, surname,age, number of brother has, number of siste

ID: 3594747 • Letter: Q

Question

Q.1 Ask user for first name, surname,age, number of brother has, number of sister has.

then count function that count number of brother and number of sister and count both number of brother and sister

and requirments, function does not accept the negitive value of number and if user put negitve value then ask again to put correct value and write all this date in text file and meanwhile display after finishing all process?

Q.2 Read first name, surname, email from text file. then have to display the data using first name + surname+ age+email. you have to count the number of people and then calculate the average age.

All of this expecting in javacode in Eclipse Neon software

Explanation / Answer

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

class Main{
  
// writes to file
public static void print(String n, String sname, int a, int b, int s)throws IOException
{
File file = new File("output.txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
  
String op = "Name is "+ n + " ";
op += "Surname is "+ sname + " ";
op += "Age is "+ a + " ";
op += "Brothers count is "+ b + " ";
op += "Sisters count is "+ s + " ";
op += "Together Brothers and Sisters are " + ( b+s );
  
writer.write(op);
  
writer.flush();
writer.close();
  
System.out.println("Done writing to file!");
  
System.out.printf("Brothers: %d ",b);
System.out.printf("Sisters: %d ",s);
System.out.printf("Brothers and Sisters: %d ",b+s);
}
  
public static void main(String[] args)throws IOException
{
// declaring variables
String name, surname;
int age=-1, brothers=-1, sisters=-1;
Scanner sc = new Scanner(System.in);
  
// taking user input
System.out.print("Enter name: ");
name = sc.nextLine();
  
System.out.print("Enter surname: ");
surname = sc.nextLine();
  
while(age<0)
{
System.out.print("Enter age: ");
age = sc.nextInt();
}
  
while(brothers<0)
{
System.out.print("Enter number of brothers you have: ");
brothers = sc.nextInt();
}
  
while(sisters<0)
{
System.out.print("Enter number of sisters you have: ");
sisters = sc.nextInt();
}
  
print(name, surname, age, brothers, sisters);
}
}
/*SAMPLE OUTPUT
Enter name: Chegg
Enter surname: India
Enter age: 23
Enter number of brothers you have: 2
Enter number of sisters you have: 3
Done writing to file!
Brothers: 2
Sisters: 3
Brothers and Sisters: 5

Output.txt
Name is Chegg
Surname is India
Age is 23
Brothers count is 2
Sisters count is 3
Together Brothers and Sisters are 5

*/