Sie sind auf Seite 1von 12

Assessment 1

Name: Nithish D
Reg.no.:18MIS0332
Slot: L7+L8
Course: Programming in java

Factorial with constraints:

public class Factwithcons


{
public static void main(String args[])
{
int a = 10;
int i;
long fact = 1;
if(a<0)
{
System.out.println("please enter the positive number");
}
if(a>1000)
{
System.out.println("the value should be less than 1000");
}
else
{
for(i=1;i<=a;i++)
{
fact = fact*i;
}
System.out.println("value of the factorial is" + fact);
}

}
}
Hello world program:

class Myfirstjava
{
public static void main(String args[])
{
System.out.println("hello world");
}
}

Factorial program:

public class Factorial


{
public static void main(String args[])
{
int num = 3;
long factorial = 1;
for(int i=1;i<=num;i++)
{
factorial = factorial*i;
}
System.out.printf("factorial of" + num + "is" + factorial);
}
};

Object Initialization:
public class Dog
{
String name;
String breed;
int age;
String color;
public Dog(String name, String breed, int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}

public String getColor()


{
return color;
}

public String toString()


{
return("Hi my name is "+ this.getName()+
".\nMy breed,age and color are " +
this.getBreed()+"," + this.getAge()+
","+ this.getColor());
}
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
System.out.println(tuffy.toString());
}
}
Nested class:
class Oclass
{
class Inner
{
public void show()
{
System.out.println("in a nested class method");
}
}
public void out()
{
System.out.println("o class");
}
}
class Main
{
public static void main(String[] args)
{
Oclass.Inner in = new Oclass().new Inner();
Oclass p = new Oclass();
in.show();
p.out();
}
}

Polymorphism:
public class Sum {

public int sum(int x, int y)


{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

Inheritance:
class Person
{
void walk()
{
System.out.println("Can Run….");
}
}
public class Employee extends Person
{
void walk()
{
System.out.println("Running Fast…");
}
public static void main(String arg[])
{
Person p = new Employee();
p.walk();
}
}
Encapsulation:
class Student
{
private String name;
public String getName()
{
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Encapsulation
{
public static void main(String[] args)
{
Student s = new Student();
s.setName("Nithish");
System.out.println(s.getName());
}
}

One dimensional array:

class Student
{
public int roll_no;
public int marks;

public String name;


Student(int roll_no, String name,int marks)
{
this.roll_no = roll_no;
this.name = name;
this.marks = marks;
}
}

public class GFG


{
public static void main (String[] args)
{
int avg=0,total = 0;
Student[] arr;

arr = new Student[5];

arr[0] = new Student(1,"aman",97);

arr[1] = new Student(2,"vaibhav",87);

arr[2] = new Student(3,"shikar",100);


arr[3] = new Student(4,"dharmesh",0);
arr[4] = new Student(5,"mohit",0);

for (int i = 0; i < arr.length; i++)


{
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name +" "+arr[i].marks);

}
for (int i = 0; i < arr.length; i++)
total = total+arr[i].marks;
avg = total/3;
System.out.println("total is:" + total);
System.out.println("avg of 3 student:" +avg);
}
}

Multidimensional array:
class Matrixaddition
{
public static void main(String args[])
{
int arr1[][] = { {2,7,9},{3,6,1},{7,4,2} };
int arr2[][] = { {1,2,3},{1,2,3},{1,2,3} };
int arr3[][] = new int[3][3];

for (int i=0; i< 3 ; i++)


{
for (int j=0; j < 3 ; j++)
{
arr3[i][j] = arr1[i][j]+arr2[i][j];
System.out.print(arr3[i][j] + " ");}
System.out.println("\n");

}
}
}

Calculator program:
import java.util.Scanner;

class Calculator {

public static void main(String[] args) {

int num1=0, num2=0,num3=0;


Scanner scanner = new Scanner(System.in);
System.out.print("Enter an operator (+, -, *, /,%): ");
char operator = scanner.next().charAt(0);
if(operator == '%')
{
System.out.print("Enter the number for factorial:");
num1 = scanner.nextInt();

}
else
{
System.out.print("Enter first number:");
num2 = scanner.nextInt();
System.out.print("Enter second number:");
num3 = scanner.nextInt();
}
scanner.close();
int output=1;

switch(operator)
{
case '+':
output = (num2) + (num3);
break;

case '-':
output = num2 - num3;
break;

case '*':
output = num2 * num3;
break;

case '/':
output = num2 / num3;
break;
case '%':
int i;
for(i=1;i<=num1;i++)
{
output = output*i;
}
break;
default:
System.out.printf("You have entered wrong operator");
return;
}
if(operator == '%')
{
System.out.println("the factorial of"+" "+num1+" "+output);
}
else
{
System.out.println(num2+" "+operator+" "+num3+": "+output);
}
}
}
Parametrized constructor:
class Parameter
{
int i,j,k;
Parameter()
{
System.out.println("constructor without parameter");
}
Parameter(int i,int j)
{
this.i = i;
this.j = j;
System.out.println("constructor with two parameter");
System.out.println(i+j);
}
Parameter(int i, int j,int k)
{
this.i = i;
this.j = j;
this.k = k;
System.out.println("constructor with three parameter");
System.out.println(i+j+k);
}
public static void main(String[] args)
{
Parameter a = new Parameter();
Parameter b = new Parameter(2,3);
Parameter c = new Parameter(1,2,3);
}
}
Challenging exp:
import java.util.Scanner;
class Zodiac
{
int month;
int day;
void output(int x,int y)
{
month = x;
day = y;
if((month==1)&&(day<=31&&day>=20) ||(month==2)&&(day<=18))

System.out.println("Aquarius");

else if((month==2)&&(day<=29 && day>=19) || (month==3)&&(day<=20))


System.out.println("Pisces");

else if((month==3) && (day<=31 && day>=21) || (month==4) && (day<=19))


System.out.println("Aries");

else if((month==4) && (day<=30 && day>=20) || (month==5) && (day<=20))


System.out.println("Taurus");
else if((month==5) && (day<=31 && day>=21) || (month==6) && (day<=20))
System.out.println("Gemini");

else if((month==6) && (day<=30 && day>=21) || (month == 7) && (day<=22))


System.out.println("Cancer");

else if((month==7) && (day<=31 && day>=23) || (month==8) && (day<=22))


System.out.println("leo");

else if((month==8) && (day<=31 && day>=23) || (month==9) && (day<=22))


System.out.println("virgo");

else if((month==9) && (day<=30 && day>=23) || (month==10) && (day<=22))


System.out.println("libra");
else if((month==10) && (day<=31 && day>=23) || (month==11) && (day<=21))
System.out.println("scorpio");

else if((month==11) && (day<=30 && day>=22) || (month==12) && (day<=21))


System.out.println("sagittarius");

else if((month==12) && (day<=31 && day>=22) || (month == 1) && (day<=19))


System.out.println("capricon");
}

public static void main(String args[])


{
Zodiac a = new Zodiac();
Scanner s = new Scanner(System.in);
int m = s.nextInt();
int n = s.nextInt();
a.output(m,n);
}
}

Das könnte Ihnen auch gefallen