Friday, 14 September 2012

Java Practical - 4

0 comments

    Program Definition 4 ::
   
     Define a class called Product, each product has a name, a product code and manufacturer name. Define variables, methods and constructors, for the Product class. Write a class called TestProduct, with the main method to test the methods and constructors of the Product class.




class Product
{
int productCode;
String name;
String manufacturerName;

Product()
{
System.out.println("\n Product Class Constructor Called ");
}
Product( int c, String nm, String mnm )
{
System.out.println("\n Product Class Constructor Called ");

productCode = c; name = nm; manufacturerName = mnm;
}

public String getName()
{
return name;
}
public int getProductCode()
{
return productCode;
}
}

class Prog4
{
public static void main(String arg[])
{
Product p = new Product(1,"Laptop","Lenovo");

System.out.println("Product Code :: " + p.getProductCode() );
System.out.println("Product Name :: " + p.getName() );
System.out.println("Product Manufacturer :: " + p.manufacturerName );
}
}

/*
D:\JAVA>java Prog4

 Product Class Constructor Called
Product Code :: 1
Product Name :: Laptop
Product Manufacturer :: Lenovo
*/

Thursday, 13 September 2012

Java Practical - 3

0 comments



Program Definition 3 ::


Write a class called Statistics, which has a static method called average, which takes a one-dimensional array for double type, as parameter, and prints the average for the values in the array. Now write a class with the main method, which creates a two-dimensional array for the four weeks of a month, containing minimum temperatures for the days of the  week(an array of 4 by 7), and uses the average method of the Statistics class to compute and print the average temperatures for the four weeks.
 


class Statistics
{
public static double average(double arr[])
{
double total=0.0;
for(double d : arr)
{
total = total + d ;
}
return (total/arr.length);
}
}

class Prog9
{
public static void main(String args[])
{
double tempWeek[][] = {{32.0,35.7,40.2,39.9,34.2,31.2,44.4},{34.2,31.2,44.4,33.3,34.2,31.2,44.4},{30.8,31.0,35.5,31.0,34.2,31.2,44.4},{34.2,39.5,42.8,32.9,34.2,31.2,44.4}};

for(int i = 0 ; i < tempWeek.length ; i++)
{
System.out.println("week " + (i+1) + "| Average temperature is :: " + Statistics.average(tempWeek[i]));
}
}
}

/*
D:\JAVA>javac Prog9.java

D:\JAVA>java Prog9
week 1| Average temperature is :: 36.8
week 2| Average temperature is :: 36.128571428571426
week 3| Average temperature is :: 34.01428571428571
week 4| Average temperature is :: 37.028571428571425
*/

Wednesday, 12 September 2012

Java Practical - 2

0 comments


Program Definition 2 ::
 
Write a class, with main method, which declares floating point variables and observe the output of dividing the floating point values by a 0, also observe the effect of assigning a high integer value (8 digits and above) to a float and casting it back to int and printing.
   

class P2
{
public static void main(String args[])
{

System.out.println(" Program 2 \n\n");

float f1 = 2.2f;
System.out.println(" Float divide by zero :: " + (f1/0));

float f2 = 1234567890f;
System.out.println(" float value = 1234567890 :: " + f2);

int i1 = (int)f2;

System.out.println("int i1 = (int)f2  :: " + i1);


}
}


/*  OUTPUT */

D:\JAVA >java P2
 Program 2

 Float divide by zero :: Infinity
 float value = 1234567890 :: 1.23456794E9
int i1 = (int)f2  :: 1234567936



 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT