Java Assignment Construstors

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

NATIONAL UNIVERSITY OF MODERN LANGUAGES

Submitted by: Shanila Afaq (11769)


Submitted to: Miss Bushra khan
Class: BSSE 2 A
Assignment no: 1

NATIONAL UNIVERSITY OF MODERN LANGUAGES


ISLAMABAD

5th, OCTOBER, 2018


Question 1:
Design a class to represent a Student details include the Student ID, Name of the Student, Branch,
year, location and college. Assign initial values using constructor. Calculate average of marks of 6
subjects and calculate attendance percentage. Include two methods GetAverageMarks() that can be
used to get average marks and GetAttendancePercantage() that can be used to get te percentage of
attendance.

Program:

package javaapplication9;

public class studentdet{


int id ,year;
String name;
String branch;
String address;
String college;
studentdet(){
name="SHANILA AFAQ";
id=070;
college="NUML";
branch="islamabad";
address="H-8, khayaban e johar";
year=2018;
}
double getattendencepercentage(){
double s1=90 ,s2=96,s3=98,s4=99,s5=93,s6=94;
return (s1+s2+s3+s4+s5+s6)/6;
}
double getaveragemarks(){
int presentdays=95;
int totaldays=110;
double attdpercentage=0;
return attdpercentage=(presentdays*100)/totaldays;
}
}
class Studentdetails{
public static void main (String args[]){
System.out.println("****STUDENT DETAILS****");
studentdet obj=new studentdet();
String n=obj.name;
System.out.println("student name is: "+n);
int i=obj.id;
System.out.println(" student id is: "+i);
String c=obj.college;
System.out.println("college name is: "+c);
String b=obj.branch;
System.out.println("branch is: "+b);
String a=obj.address;
System.out.println("address is: "+a);
int y=obj.year;
System.out.println("year is: "+y);
double attendence=obj.getattendencepercentage();
System.out.println("attendence percentage is: "+attendence);
double averagemarks=obj.getaveragemarks();
System.out.println("average marks are: "+averagemarks);
}
}

Input:
Output:
Question 2:
Design a class to find factorial of a number. The class should have a factorial method and should
calculate factorial for user supplied value.

Program:

package javaapplication7;
import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {


Scanner obj1= new Scanner(System.in);
System.out.print("Enter the number to calculate its factorial: ");
int n = obj1.nextInt();
int result = factorial(n);
System.out.println("The factorial of " + n + " is " + result);
}

public static int factorial(int n) {


int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
}

Input:
Output:

You might also like