Advertisement
glee20

Methods

Oct 8th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.util.Calendar;
  2. import java.util.Scanner;
  3. public class methodsEg
  4. {
  5.     public static void welcome(){
  6.            System.out.println("Welcome!");
  7.     }
  8.    
  9.     public static int ageIn2020(int a){
  10.            a+=2;
  11.            return a;
  12.     }
  13.    
  14.     public static int ageIn (int age, int targetYear){
  15.            int year = Calendar.getInstance().get(Calendar.YEAR);
  16.            age+=targetYear-year;
  17.            System.out.println("Current year is " + year);
  18.            return age;
  19.     }
  20.    
  21.     public static String firstLetter(String n){
  22.            String first = n.substring(0,1);
  23.            return first;
  24.     }
  25.    
  26.     public static double circleArea(double radius){
  27.             double area = 3.14159*radius*radius;
  28.             return area;
  29.     }
  30.    
  31.      public static void main(String args[] ) {
  32.        int age;
  33.        String name;
  34.        int year;
  35.        double radius;
  36.        
  37.        welcome();
  38.        System.out.println("");
  39.        Scanner input = new Scanner(System.in);
  40.        System.out.println("Enter age: ");
  41.        age=input.nextInt();
  42.        
  43.        System.out.println("Age in 2020 is "+ ageIn2020(age));
  44.        System.out.println("");
  45.        
  46.        System.out.println("Enter target year: ");
  47.        year=input.nextInt();
  48.        System.out.println("Age in "+year+" will be "+ ageIn(age,year));
  49.        System.out.println("");
  50.        
  51.        System.out.println("Enter name: ");
  52.        name=input.next();
  53.        System.out.println("The first letter of your name is "+ methodsEg.firstLetter(name));
  54.        System.out.println("");
  55.        
  56.        System.out.println("Enter radius of circle: ");
  57.        radius=input.nextDouble();
  58.        System.out.println("The area of the circle is " + circleArea(radius));
  59.        System.out.println("");
  60.        
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement