Question

In: Computer Science

Write a java program which asks the user to enter name and age and calls the...

  1. Write a java program which asks the user to enter name and age and calls the following methods:
    1. printName(): Takes name as parameter and prints it 20 times using a while loop.
    2. printAge(): Takes age as parameter and prints all the numbers from 1 up to age.

  1. Write a java program that will print first 10 multiples of 3 in a single line.

Solutions

Expert Solution

1)Working of the code

  • Using a Scanner object,name and age are read
  • nextLine() method of Scanner class is used to read a line of string
  • nextInt() method of scanner class is used to read an integer
  • printName() and printAge() functions are called
  • In printNmae() function,an integer i is declared and initialized with 1
  • while loop executes its body as long as i is less than or equal to 20
  • Inside loop body, name is printed and i is incremented by 1
  • In printAge() function,an integer i is declared and initialized with 1
  • while loop executes its body as long as i is less than or equal to age
  • Inside loop body, i is printed and incremented by 1

source code

//Importing scanner class to accept user input
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //Creating Scanner object to read user input
        Scanner sc=new Scanner(System.in);
        //prompt user to enter name
        System.out.print("Enter name: ");
        //Reading the name
        String name=sc.nextLine();
        //prompt user to enter age
        System.out.print("Enter age: ");
        //Reading the age
        int age=sc.nextInt();
        //calling printName() method
        printName(name);
        //calling printAge() method
        printAge(age);
    }
    //printName() method definition
    public static void printName(String name)
    {
        //initializing  variable i to 1
        int i=1;
        //checking i is less than or equals to 20.if yes execute the loop body
        //if not, exit from the loop
        while (i<=20)
        {
            //printing name
            System.out.println(name);
            //incrementing i by 1
            i++;
        }
    }
    //printAge() method definition
    public static void printAge(int age)
    {
        //initializing  variable i to 1
        int i=1;
        //checking i is less than or equals to age.if yes execute the loop body
        //if not, exit from the loop
        while (i<=age)
        {
            //printing i
            System.out.println(i);
            //incrementing i by 1
            i++;
        }
    }
}

Screen shot of the program

Screen shot of the output

2) Working of the program

  • Declare an integer variable i and initialize it with 1
  • While loop executes its body as long as i is less than or equals to 10
  • Inside loop,System.out.print() method is used to print multiple of 3 with a space after.
  • increment i by 1 each time
  • System.out.print() does not move to new line when each time it execute.It print values in the same line

Source code

public class Main {
    public static void main(String[] args) {
        //declare integer variable i and initialize with 1
        int i = 1;
        //Execute loop body as long as i is less than or equal to 10
        while (i <= 10) {
            //print multiple of i and put a space
            System.out.print(3 * i+" ");
            //increment i by 1
            i++;
        }
    }
}

Screen shot of the code

Screen shot of the output


Related Solutions

Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
Write a program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for...
Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for five stores. The program should then display a bar chart comparing each store’s sales. Create each bar in the bar chart by displaying a row or asterisks. Each asterisk should represent $100 of sales. You must use a loop to print the bar chart!!!! If the user enters a negative value for the sales amount, the program will keep asking the user to enter...
Write a Python program that asks the user to enter a student's name and 8 numeric...
Write a Python program that asks the user to enter a student's name and 8 numeric tests scores (out of 100 for each test). The name will be a local variable. The program should display a letter grade for each score, and the average test score, along with the student's name. Write the following functions in the program: calc_average - this function should accept 8 test scores as arguments and return the average of the scores per student determine_grade -...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric assignment scores (out of 100 for each assignment). The program should output the student's name, a letter grade for each assignment score, and a cumulative average for all the assignments. Please note, there are 12 students in the class so your program will need to be able to either accept data for 12 students or loop 12 times in order to process all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT