import java.util.Scanner;
class calculateCost {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);
int weight=0;
String method=null;
double cost=0.0;
System.out.println("Choose the selected shipping method");
System.out.println("A=Air");
System.out.println("T=Truck");
System.out.println("M=Mail");
String choice=s.next();
if(choice.equals("A"))
{
method="Air";
System.out.println("Enter weight :");
weight=s.nextInt();
if (weight <=8)
cost=2.00;
else if (weight<=16)
cost=3.00;
else
cost=4.50;
}
else if(choice.equals("T"))
{
method="Truck";
System.out.println("Enter weight :");
weight=s.nextInt();
if (weight <=8)
cost=1.50;
else if (weight<=16)
cost=2.35;
else
cost=3.25;
}
else if(choice.equals("M"))
{
method="Mail";
System.out.println("Enter weight :");
weight=s.nextInt();
if (weight <=8)
cost=0.50;
else if (weight<=16)
cost=1.50;
else
cost=2.15;
}
else
System.out.println("Thank You");
System.out.println("Method\tWeight\tCost");
System.out.println("=====================");
Package p = new Package(weight,method,cost);
System.out.println( p.display());
}
}
Tuesday, 17 March 2009
String Compare strcmp
//Program to key in the password 3 times
#include
#include
#define mypassword "abc123"
void main()
{
char password [20] ;
int i ;
for( i=0; i <3; ++i)
{
printf("\nEnter security password : ");
gets(password);
if (strcmp(password,mypassword)==0)
{
printf("\nCorrect\n");
break;
}
}
if (i==3)
printf("\nYou have key in the password 3 time\n");
}
#include
#include
#define mypassword "abc123"
void main()
{
char password [20] ;
int i ;
for( i=0; i <3; ++i)
{
printf("\nEnter security password : ");
gets(password);
if (strcmp(password,mypassword)==0)
{
printf("\nCorrect\n");
break;
}
}
if (i==3)
printf("\nYou have key in the password 3 time\n");
}
Monday, 16 March 2009
C+ Super Array !!!
#include
#include
#define myfruit "grapes"
void main()
{
char name [20];
char string [20] = "Hello";
char string2 [20] = "How are you";
char dummy[20];
char fruit [20];
printf("\nEnter name : ");
scanf ("%s",&name);
gets(dummy); // to read the enter key and store in the string dummy
printf("\nYour name is %s",name);
printf("\nEnter your name again : ");
gets(name);
printf("\nYour nane is %s\n",name);
strcat(string,name);
printf("\nNow in string is %s",string);
printf("\nThe number of characters in name is %d",strlen(name));
strcpy(string,string2);
printf("\nNow in string is %s",string);
printf("\nThe value return is %d",strcmp("Apple","Papaya"));
printf("\nThe value return is %d",strcmp("Papaya","Apple"));
printf("\nThe value return is %d",strcmp("Apple","Apple"));
printf("\nGuess my favourite fruit ?");
gets(fruit);
if (strcmp(fruit,myfruit)==0)
printf("\nWell done! You guess it right!\n");
else
printf("\nSorry! %s is not my favourite fruit\n",fruit);
}
#include
#define myfruit "grapes"
void main()
{
char name [20];
char string [20] = "Hello";
char string2 [20] = "How are you";
char dummy[20];
char fruit [20];
printf("\nEnter name : ");
scanf ("%s",&name);
gets(dummy); // to read the enter key and store in the string dummy
printf("\nYour name is %s",name);
printf("\nEnter your name again : ");
gets(name);
printf("\nYour nane is %s\n",name);
strcat(string,name);
printf("\nNow in string is %s",string);
printf("\nThe number of characters in name is %d",strlen(name));
strcpy(string,string2);
printf("\nNow in string is %s",string);
printf("\nThe value return is %d",strcmp("Apple","Papaya"));
printf("\nThe value return is %d",strcmp("Papaya","Apple"));
printf("\nThe value return is %d",strcmp("Apple","Apple"));
printf("\nGuess my favourite fruit ?");
gets(fruit);
if (strcmp(fruit,myfruit)==0)
printf("\nWell done! You guess it right!\n");
else
printf("\nSorry! %s is not my favourite fruit\n",fruit);
}
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());
}
}
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());
}
}
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;
}
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;
}
Subscribe to:
Posts (Atom)