0% found this document useful (0 votes)
61 views16 pages

.Java Oop - 1606790064000

The document discusses several Java programming tasks including declaring variables, taking user input, using control statements, loops, string manipulation methods, arrays, arithmetic operations, and displaying student details. The tasks cover basic Java concepts and demonstrate how to write simple Java programs.

Uploaded by

KetanPatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views16 pages

.Java Oop - 1606790064000

The document discusses several Java programming tasks including declaring variables, taking user input, using control statements, loops, string manipulation methods, arrays, arithmetic operations, and displaying student details. The tasks cover basic Java concepts and demonstrate how to write simple Java programs.

Uploaded by

KetanPatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OOP With Java

Ketan k rajani Assignment 01


MCA Sem 03 Date: - 29-11-2020

Q.1 Write A Program to Following Task.

Task 1: Declare one static variable.

class Example
{
static int Enrollment Number;
static String Name;
static void disp(){
System.out.println("Enrollment Number: "+em);
System.out.println("Name is: "+name);
}
public static void main (String args[])
{
em =20206121999 =0031303172796
name = "Yash Garnara"; name = "ketan rajani";
disp();
}}
O/P: -
Enrollment No: 003103172796
Name is: ketan rajani
 Task 2: Take at least one input from user and store it in variable

import java.util.*;
class I2
{
public static void main (String[] args)
{
Scanner s1 = new Scanner (System.in);
System.out.println("Enter your your Stream");
String f = s1.next();
System.out.println("Enter your University name");
String l = s1.next();
System.out.println("I am Student" + f + l);
}
}

Output: -
Enter your Stream
MCA
Enter your University name
R.K.U.
I am Student MCA R.K.U
Task 3: Add at least one control statement.

public class Main


{
public static void main (String args[])
{
int a = 100;
if (a > 300)
System.out.println("a is greater than 300");
else
System.out.println("a is less than 300");
}
}

Output: -

A is less than 300


Task 4: Add at least one loop

class whileLoop
{
     public static void main(String args[])
   {
         int x = 1;
         while (x <= 5)
         {
             System.out.println("Value of x:" + x);
             x++;
         }
     }
}

Output: -

Value of x:1
Value of x:2
Value of x:3
Value of x:4
value of x:5x
(2) Write a program that includes multiple string manipulation methods such
as length (), concat (), substring (), etc.

Task 1: Create a String object using literal.


Task 2: Create a String object using new keyword.
public class StringObjectByNewKeyword
{
public static void main(String[] args)
{
String str = new String("Hello, World");
System.out.println(str);
}
}

Task 4: Concatenate two Strings.


class TestStringConcatenation1
{  
  public static void main(String args[])
{  
    String s="Hello "+"How Are You";  
    System.out.println(s);
  }  
}  

Output: -
Hello How are You
 Task 5: Perform any three String manipulation methods.

public class Sample_String


{
public static void main(String[] args)
{
String str1 = "Hi";
String str2 = "Hello";
String str3 = str1.concat(str2);
System.out.println(str3);
String str4 = str1 + str2;
System.out.println(str4);
}
}

Output: -

Hi Hello
Hi Hello
(3) Write a Java program to work with array.

Task 1: Declare one dimensional array and initialize it element wise like array [0], array
[1], etc.

Class onearray
{
public static void main(String args[])
{    
int[] a=new int[3];  
a[0]=10;  
a[1]=20;  
a[2]=30;  

System.out.println("One dimensional array");    


System.out.println(a[0]);    
System.out.println(a[1]);    
System.out.println(a[2]);    
}
}

Output: -
One dimensional array
10
20
30
Task 2: Declare one dimensional array using new keyword and initialize using for loop.

package arrayDemo;
import java.util.Arrays;
public class ResultListDemo
{
public static void main(String[] args)
{
int resultArray[] = new int[6];
resultArray[0]=1;
resultArray[1]=2;
resultArray[2]=3;
resultArray[3]=4;
resultArray[4]=5;
resultArray[5]=6;
System.out.println("First"+ resultArray[0]);
System.out.println("Second "+ resultArray[1]);
System.out.println("Third "+ resultArray[2]);
System.out.println("Fourth"+ resultArray[3]);
System.out.println("Fifth"+ resultArray[4]);
System.out.println("Sixth "+ resultArray[5]);
}
}
Output: -
First 1
Second 2
Third 3
Fourth 4
Fifth 5
Sixth 6
Task 3: Declare one dimensional array and input elements from user.
Class Array
Class Array
{
public static void main(String args[])
{
int[] a=new int[3];
a[0]=12;
a[1]=31;
a[2]=08;
System.out.println("One dimensional array elements are");
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
Output: -
One dimensional array elements are
12
31
08
Task 4: Declare two-dimensional array and display 3X3 matrix in output.

public class Yash


{
public static void main(String[] args)
{
final int[][] matrix =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}
}
The matrix is:
123
456
789
Q.4 WAP to input any number n and print the table of this number.

Program: -
import java.util.Scanner;
public class Exercise1
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.print("Input a number: ");
int num1 = in.nextInt();
for (int i=0; i< 10; i++)
{
System.out.println(num1 + " x " + (i+1) + " = " + (num1 * (i+1)));
}
}
}
Output: -
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
6. Create a Java program to perform all arithmetic operations on command line
arguments.

public class Arithmetic


{
public static void main(String[ ] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int rem = a % b;
int sub = a - b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + mul);
System.out.println(a + " / " + b + " = " + div);
System.out.println(a + " - " + b + " = " + sub);
System.out.println(a + " % " + b + " = " + per);
}
}
7. Create a Java program to input student's basic details and display the same in proper
format.

Program: -
import java.util.Scanner;

public class GetStudentDetails


{
public static void main(String args[])
{
String name;
int roll, math, phy, eng;
Scanner SC=new Scanner(System.in);
System.out.print("Enter Name: ");
name=SC.nextLine();
System.out.print("Enter Roll Number: ");
roll=SC.nextInt();
System.out.print("Enter marks in Maths, Physics and English: ");
math=SC.nextInt();
phy=SC.nextInt();
eng=SC.nextInt();
int total=math+eng+phy;
float perc=(float)total/300*100;
System.out.println("Roll Number:" + roll +"\tName: "+name);
System.out.println("Marks (Maths, Physics, English): "
+math+","+phy+","+eng);
System.out.println("Total: "+total +"\tPercentage: "+perc);
}
}

You might also like