Thursday, 5 March 2009

Inheritance

public class Event {

int NumGuests;

public Event() {
setNumGuests(0);
}

public Event(int g){
setNumGuests (g);
}

private void setNumGuests(int g){
NumGuests = g;
}

public int getNumGuests(){
return NumGuests;
}

public String displayNumGuests () {
return "Number of Guests: "+NumGuests;
}

}

public class DinnerEvent extends Event {

int dinnerChoice;

public DinnerEvent() {
super (0);
setDinnerChoice(' ');
}

public DinnerEvent(int g ,int c) {
super (g);
setDinnerChoice(c);
}


private void setDinnerChoice(int c) {
dinnerChoice=c;
}

public String getDinnerChoice() {
String msg=null;
switch(dinnerChoice) {
case 1 : msg = "Dinner choice is Beef"; break;
case 2 : msg = "Dinner choice is Chicken"; break;
case 3 : msg = "Dinner choice is Fish"; break;
}
return msg;
}

public String displayDinnerEvent() {
return displayNumGuests() +"\n"+getDinnerChoice();
}
}


import java.util.Scanner;
class testevent{
public static void main(String agrs[]) {
Scanner s = new Scanner (System.in);
System.out.println ("Enter the number of quests for the event :");
int nofGuest=s.nextInt();

System.out.println("Dinner Choice: \n1=Beef\n2=Chicken\n3=Fish\nEnter Option: ");
int choice=s.nextInt();
DinnerEvent de= new DinnerEvent(nofGuest,choice);
System.out.println(de.displayDinnerEvent());
System.out.println("\nCalling a method in Event Class--->\n"+de.displayNumGuests());
}
}

N-e-r-D for life XD

Monday, 2 March 2009

Recursive Function

#include
int sub (int,int);

void main()
{
int a = 9;
int b = 3;

printf("The answer is %d \n" ,sub(a,b));
}

int sub (int a,int b)
{
if (b==1)
return a;
else
return sub(a,b-1)*9;
}

Thursday, 19 February 2009

Method 2 : Test Person

import java.util.Scanner;
class TestPerson {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);
String name;
int age,g;
boolean gender;
double salary;
//Person p;

System.out.println("Name\tAge\tGender\tSalary\tNet Pay");
System.out.println("-----------------------------------------");
for (int i=1; i < 3; i++) {
System.out.print("Enter name: ");
name = s.next();
System.out.print("Enter age: ");
age = s.nextInt();
System.out.print("Enter gender: (1-Male, 2-Female): ");
g = s.nextInt();
if (g==1)
gender = true;
else
gender = false;
System.out.print("Enter salary: ");
salary = s.nextDouble();
Person p = new Person(name,age,gender,salary);
System.out.println( p.display() );
} // for
} // main()
} //class

Method 1: Person

public class Person {
private String name;
private int age;
private boolean gender;
private double salary;

public Person() { // default constructor
setPerson(null,0,false,0.0); //intialize the data for this object
}
public Person(String n, int a, boolean g, double s) { //proper constructor
setPerson(n,a,g,s);
}
private void setPerson(String n, int a, boolean g, double s) { //set method
name = n; age = a; gender = g; salary = s;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender ? "Male" : "Female";
}
public double getSalary() {
return salary;
}
public double getNetSalary() {
return getSalary() - (getSalary()*0.1);
}
public String display() {
return getName()+"\t"+age+"\t"+getGender()+"\t"+salary+"\t"+getNetSalary();
}
}

Tuesday, 17 February 2009

LAB

// This is program is to calculate the total mark of exam and assignment

#include

void main ()
{
int mark1 ,mark2 ;
float total1,total2,sum;

printf ("Enter the total mark of the assignment \n");
scanf ("%d", &mark1);

printf ("Enter the total exam of the assignment \n");
scanf ("%d", &mark2);

total1=(mark1*40)/100;

if (total1 <16)
printf ("You have to RETAKE the paper \n");
else
printf ("You have pass the paper \n");

total2=(mark2*60)/100;

if (total1 <24)
printf ("You have to RESIT the paper \n");
else
printf ("You have pass the exam \n");

sum=total1=total2;


if (sum >= 70)
printf ("Grade A %f \n",sum);
else if (sum >= 60 && sum <=69)
printf ("Grade B %f \n",sum);
else
printf ("Grade C %f \n",sum);
}

Monday, 16 February 2009

Counting Grade while repeating

// This program is to display the average in grade

#include
void main ()
{
int x;
int mark=0;
int sum=0;
float average=0;
char ans;

do {
for ( x = 0; x <5 ; x++)
{
printf("Enter 5 mark \n");
scanf ( "%d",&mark);
sum=sum+mark;
average = sum/5;
}




if (average>=70)
printf ("Grade A \n");

else if (average>=60 && average <=69)
printf ("Grade B \n");

else if (average>=50 && average <=59)
printf ("Grade C \n");

else if (average>=40 && average <=49)
printf ("Grade D \n");

else if (average<=40)
printf ("Grade E \n");

mark=0;
sum=0;
average=0;

printf ("Do you wan to continue :D \n");
getchar();
scanf ("%c",&ans);

}while ( ans =='y' || ans == 'Y');

}