Experiment 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Experiment - 3

Class OOPs

Type Lab

Name: Muditt Sharmma


Roll No.: R2142210506
SAP ID: 500091725
Branch: Btech. CSE spl (CSF)

AIM:
1. Write a program to take input (a number) of a month (1 - 12) and print its equivalent name of the month. ( e.g 1 to Jan,
2 to Feb. 12 to Dec.) Use Scanner class for user input( Hint-use switch case).

2. Write a program to add two numbers using command line arguments.

3. Write a program to implement a command line calculator.

4. Write a program that takes students’ info from the user (using Scanner class)  and displays output on a black screen.
Use appropriate mutator and accessor.  setInfo(),getInfo().

1) Write a program to take input (a number) of a month (1 - 12) and print its
equivalent name of the month. ( e.g 1 to Jan, 2 to Feb. 12 to Dec.) Use
Scanner class for user input( Hint-use switch case).

import java.util.*;

class First4{
public static void main(String [] args){
System.out.println("Name: Muditt Sharmma,Sap ID : 500091725, Roll No.: R2142210506");

Scanner input = new Scanner(System.in);

System.out.print("Enter the number for the month: ");


int i = input.nextInt();

switch(i){
case 1:
System.out.println("January");
break;
case 2:
System.out.println("Febuary");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");

Experiment - 3 1
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("There is an error with the user input");
}

}
}

Algorithm:
1) To use the Scanner class, import the java.util. * module.
2) In the main method, make the scanner object.
3) Using the scanner object, ask the user for inputs and store them in the variable “i”.
4) Use a "i" expression in a switch case statement.
5) Write cases 1 through 12 and statements to print the names of the months that go with them.

2) Write a program to add two numbers using command line arguments.

class Summision{
public static void main(String args[]){
System.out.println("Name: Muditt Sharmma,Sap ID : 500091725, Roll no. : R2142210506");
Double a=Double.parseDouble(args[0]);
Double b=Double.parseDouble(args[1]);
Double c=a+b;
System.out.println(c);
}
}

Algorithm:
1. Create a summary for your class

2. Create up with the main way

3. Create two variables called "a" and "b."

4. Change the string variables to double variables.

5. Give c the value that is equal to the sum of a and b.

Experiment - 3 2
6. Print the output

3) Write a program to implement a command line calculator.

class Calculator{
public static void main(String args[]){
System.out.println("Name: Muditt Sharmma, Sap ID : 500091725, Roll no. : R2142210506");
Double a,b,Sum,Dif,Prod,Quoet,Rem;
a=Double.parseDouble(args[0]);
b=Double.parseDouble(args[1]);

Sum=a+b;
Dif=a-b;
Prod=a*b;
Quoet=a/b;
Rem=a%b;

System.out.println("the sum of the no is:"+Sum);


System.out.println("the subtraction of the no is:"+Dif);
System.out.println("the Multiplication of the no is:"+Prod);
System.out.println("the Division of the no is:"+Quoet);
System.out.println("the Remainder of the no is:"+Rem);

}
}

Algorithm:
1. Create a class calculator

2. Create the main method

3. Create create 7 double data-type variables a,b,Sum,Dif, Prod, Quoet, Rem

4. Convert the string input from the user to double

5. Assign Sum=a+b

6. Assign Dif=a-b

7. Assign Prod=a*b

8. Assign Quoet=a/b

9. Assign Rem=a%b

10. Print Sum, Dif, Prod, Quoet, Rem

Experiment - 3 3
4) Write a program that takes students’ info from the user (using Scanner
class)  and displays output on a black screen. Use appropriate mutator and
accessor.  setInfo(), getInfo().

import java.util.*;

class StudentRec{
static String Name;
static int Rollno;
static String Branch;
static Scanner input;

public static void main(String[] args){


System.out.println("Name: Muditt Sharmma, Sap ID : 500091725, Roll no. : R2142210506");
input = new Scanner(System.in);
setinfo();
getinfo();
}

public static void setinfo(){


System.out.println("Name:");
Name=input.next();
System.out.println("Roll no:");
Rollno=input.nextInt();
System.out.println("Branch:");
Branch=input.next();
}

public static void getinfo(){


System.out.println("Student Details:");
System.out.println("Name:"+Name);
System.out.println("Roll no:"+Rollno);
System.out.println("Branch:"+Branch);
}
}

Algorithm:
1. Load the java.util.* module.

2. Create a class called StudentRec.

3. Create a string variable named Name that is static.

4. Create a static int variable Rollno

5. Create a string variable Branch

6. Create a static scanner, input

Experiment - 3 4
7. Create up with the main way

8. Print the required parts

9. Create a new scanner instance, enter

10. Make and use the setinfo() function to get the input.

11. Make the getinfo function and use it to print out the information from the setinfo() function.

Experiment - 3 5

You might also like