/************************************************************************* * Compilation: javac JavaLab1.java * Execution: java JavaLab1 * Authors: Odile wolf, Olusola Samuel-Ojo * Date created: Jan 15th 2014 * Last update date: Sep 13th 2017 * You will modify the code to ask the user for their age and the age of their child, and output how old the mother * was when the child was born * % java JavaLab1 * What is your name ? Jane * Jane, what is your age? 30 * What is your child's age? 10 * you were 20 when your child was born. * NOTE: I have put a lot of comments to guide you in the code. *************************************************************************/ import java.io.*; public class JavaLab1 { public static void main(String[] args) { // first we define our input streams. InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); String sName; Integer motherAge; // we catch exceptions if some are thrown. // an exception would be entering a string when a number is expected try { System.out.println("what is your name?"); // we read a string from the stream sName = reader.readLine(); System.out.println(sName+" ,what is your age?"); // parseint will transform a string i.e."123" into a number 123 motherAge = Integer.parseInt(reader.readLine()); // this will print "Jane, you are 30", if the inputs were "Jane" and 30 System.out.println(sName + " ,you are "+ motherAge); } catch (IOException e){ System.out.println("Error reading from user"); } } }
USING THIS JAVA CODE
You will then modify the program to ask for the age of the mother, the age of the child and will deduce the age the mother was when she had the child.
Here I am writing the modified code as per your requirements;
I will highlight the changes made in code with blue color.
import java.io.*;
public class JavaLab1{
public static void main(String[] args) {
// first we define our input streams.
InputStreamReader input = new
InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String sName;
Integer motherAge;
int sonAge;
// we catch exceptions if some are thrown.
// an exception would be entering a string when a number is
expected
try
{
System.out.println("what is your name? "); // we read a string from
the stream
sName = reader.readLine();
System.out.println(sName+" ,what is your age? ");
// parseint will transform a string
i.e."123" into a number 123
motherAge = Integer.parseInt(reader.readLine());
System.out.println("What is your
child's age? ");
sonAge = Integer.parseInt(reader.readLine());
// this will print "Jane, you are 30". if the inputs were "Jane"
and 30
System.out.println(sName + " ,you are "+ motherAge);
System.out.println(sName + " you were
"+ (motherAge-sonAge) + " when your child was born.");
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
}
To get the mother's age when she had child. we can subtract the child's age from the mother's age to get our answer.
To execute this code you can copy the code and use the commands you have mentioned and you will get the output.
Note: If you have any doubts you can ask me in the comments section.
And if you like the answer please give it a thumbs up.
/************************************************************************* * Compilation: javac JavaLab1.java * Execution: java JavaLab1 * Authors: Odile wolf, Olusola Samuel-Ojo *...
This is a JAVA language The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Note: DebugData2.txt does not need to be edited. It is used by the program. DebugData2.txt 435-9845 239-9845 981-9883 384-5656 875-3784 874-8120 DebugThirteen2.java // Program reads in a file of phone numbers without area codes // inserts "(312)...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...
In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...
Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...
Can someone please help me? I'm having trouble with this assignment and have been stuck trying to figure it out for over an hour. Here's the assignment details below: For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents...
Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that you do. Please make sure it is very efficient and doesnt run out of memory. A demo code required for the question is given below. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to...
Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...
IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows: bmi = kilograms / (meters2) where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...
package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 { public static void main (String [] args) { // ============================================================ // Step 2. Declaring Variables You Need // These constants are used to define 2D array and loop conditions final int NUM_ROWS = 4; final int NUM_COLS = 3; String filename = "Input.txt"; // A String variable used to save the lines read from input...