Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Wednesday, 19 October 2011

Java program to find sum, average and modulus by 3, of three numbers by(aol.com)

import java.util.*;
class average
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double a,b,c,sum=0;
double mod,avg;
System.out.print ("\n\t Enter first number = ");
a=in.nextDouble();
System.out.print ("\n\t Enter second number = ");
b=in.nextDouble();
System.out.print ("\n\t Enter third number = ");
c=in.nextDouble();
sum=a+b+c;
avg=sum/3;
mod=sum%3;




System.out.println ("\n\t Sum of the numbers = "+sum);
System.out.println ("\n\t Average of the numbers = "+avg);
System.out.println ("\n\t Modulus of the sum of numbers by 3 = "+mod);
}
}

Java program which calculates next number by adding all previous numbers using exception handling (by Adler Lawn Service, LLC)

import java.util.*;

class EverySum
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b=0;
System.out.println ("\n\t The number must be 50 or less");
System.out.print ("\n\t Enter the nth number = ");
a=in.nextInt();
try
{
if (a>50)
check.test();
else
{
for (int i=1;i<a;++i)
{
b+=i;
System.out.print ("\t "+b);
}
}

}
catch (IndexOutOfBoundsException e)
{
System.out.println ("\n\t Invalid number: "+e);
}


}
}

class check
{
static void test() throws IndexOutOfBoundsException
{
throw new IndexOutOfBoundsException (">50");
}
}

Java program to display ifno of different cars using abstract class (by noor diana fauzi)

import java.util.*;
class CarPrice
{
public static void main (String args[])
{
Ford fr = new Ford();
fr.getcarmaker();
fr.setPrice();
Chevy ch = new Chevy();
ch.getcarmaker();
ch.setPrice();
fr.displyinfo();
ch.displyinfo();
}
}

abstract class Auto
{
abstract void getcarmaker();
abstract void setPrice();
}

class Ford extends Auto
{
Scanner in = new Scanner (System.in);
double price;
String maker;
void getcarmaker()
{
System.out.print ("\n\t Enter the name of car maker = ");
maker=in.nextLine();
}

void setPrice()
{
System.out.print ("\n\t Enter price of "+maker+" car = $");
price=in.nextDouble();
}

void displayinfo()
{
System.out.println ("\n\t Car maker = "+maker);
System.out.println ("\n\t Car Price = $"+price);
}
}

class Chevy extends Auto
{
Scanner in = new Scanner (System.in);
double price;
String maker;
void getcarmaker()
{
System.out.print ("\n\t Enter the name of car maker = ");
maker=in.nextLine();
}

void setPrice()
{
System.out.print ("\n\t Enter price of "+maker+" car = $");
price=in.nextDouble();
}

void displayinfo()
{
System.out.println ("\n\t Car maker = "+maker);
System.out.println ("\n\t Car Price = $"+price);
}
}

Java program to find area of a square and rectangle using abstract class (by noor diana fauzi)

import java.util.*;
class area
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double base,height;
System.out.print ("\n\t Enter base of the Rectangle = ");
base=in.nextDouble();
System.out.print ("\n\t Enter height of the Rectangle = ");
height=in.nextDouble();

double side1,side2;
System.out.print ("\n\t Enter length of side1 of the Square = ");
side1=in.nextDouble();
System.out.print ("\n\t Enter length of side2 of the Square = ");
side2=in.nextDouble();

Rectangle ob1 = new Rectangle (base,height);
Square ob2 = new Square (side1,side2);
ob1.area();
ob2.area();
}
}


abstract class Shape
{
abstract void area();
}

class Rectangle extends Shape
{
double base,height,a;
Rectangle(double b, double l)
{
base=b;
height=l;
}
void area()
{
a=base*height;
System.out.println ("\n\t Area of Rectangle = "+a);
}
}

class Square extends Shape
{
double side1,side2,a;
Square (double s1, double s2)
{
side1=s1;
side2=s2;
}
void area()
{
a=side1*side2;
System.out.println ("\n\t Area of Square = "+a);
}
}