Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Saturday 29 June 2013

Java program based on constructor

Program to keep record of height and weight of people.

Code :
import java.io.*;
class Person{
    float height;
    float weight;
    Person(float h,float w){
        height=h;
        weight=w;
    }
    void putPersonData(){
        System.out.print("\nHeight : "+height+" Weight : "+weight);
    }
};
class PersonCount{
    static int count;
    static Person p[]=new Person[10];
    static void showCount(){
        for(int i=0;i<10;i++){
            p[i].putPersonData();
            if(p[i].height>170 && p[i].weight<50){
                count=count+1;            
            }
        }
        System.out.print("\n\nTotal Count (weight<50kg AND height>170cm ) : "+count+"\n");
    }
    public static void main(String args[]){
        p[0]=new Person(173,40);
        p[1]=new Person(170,30);
        p[2]=new Person(168,55); 
        p[3]=new Person(172,55);
        p[4]=new Person(169,55);
        p[5]=new Person(175,70);
        p[6]=new Person(171,45);
        p[7]=new Person(172,55);
        p[8]=new Person(168,55);
        p[9]=new Person(168,65);
        showCount();
    }
}
Output :
Height : 173.0 Weight : 40.0
Height : 170.0 Weight : 30.0
Height : 168.0 Weight : 55.0
Height : 172.0 Weight : 55.0
Height : 169.0 Weight : 55.0
Height : 175.0 Weight : 70.0
Height : 171.0 Weight : 45.0
Height : 172.0 Weight : 55.0
Height : 168.0 Weight : 55.0
Height : 168.0 Weight : 65.0

Total Count (weight<50kg AND height>170cm ) : 2


Program to maintain score card of students.

Code :
import java.io.*;
class Student{
    String name;
    Student(String nm){
        name=nm;
    }
    void putStudentName(){
        System.out.print("\n Student Name : "+name);
    }
};
class Marks{
    Student s;
    int mark1,mark2;
    Marks(String name,int m1,int m2){
        s=new Student(name);
        mark1=m1;
        mark2=m2;
    }
    void putmarks(){
        s.putStudentName();
        System.out.print("\nMark 1 : "+mark1);
        System.out.print("\nMark 2 : "+mark2);
        putAverage();  
    } 
    void putAverage(){
        float t=(mark1+mark2)/(float)2;
        System.out.print("\nAverage : "+t);
    } 
};
class Result{
    public static void main(String args[]){
        Marks m=new Marks("Maverick",80,85);
        m.putmarks();
    }
}
Output :
Student Name : Maverick
Mark 1 : 80
Mark 2 : 85
Average : 82.5
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

No comments:

Post a Comment