Thursday, 4 October 2012

Polynomial

Q3. A polynomial is represented by two fields; one is the degree and second is the coefficient. Thus 5x2+ 2x+3 is degree two and its coefficients are coeff[0]=3, coeff[1]=2 and coeff[2]=5. Design a class Polynomial to represent single-variable polynomials which contain the following functionality:
·       A zero argument constructor that initializes the degree and coefficients to zero
·       A two argument constructor that initializes both the degree and the coefficients array

Code:

public
class Coefficient {
public static int [] coeff;
public static int degree;
public Coefficient(){
degree = 0;
coeff = new int[degree + 1];
for(int i=0; i<degree+1; i++){
coeff[i]=0;
}
}
public Coefficient (int d , int[] input){
degree =d;
coeff = new int [degree +1];
for(int i=0; i<degree+1; i++){
coeff[i]= input[i];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int deg = 5;
int[] s =(1,2,3,4,5);
}
}

Thursday, 27 September 2012

FeetInches Code


Question:


FeetInches Class – Sample Program for understanding
The class has two int fields: feet and inches. Together these fields hold a distance measured in feet and inches, such as 12 feet 7 inches. The feet field holds the feet part and the inches field holds the inches part.
Method Description
Constructor #1 This constructor assigns 0 to both the feet and inches fields. (no-arg constructor)
Constructor #2 This constructor accepts two int arguments that are assigned to the feet and inches fields. The simplify method is also called.
Constructor #3 This constructor accepts a FeetInches object as its argument. The object that is being created will become a copy of the object passed as an argument. This type of constructor is sometimes referred to as a “copy constructor.”
simplify This method adjusts any set of values where the inches field is greater than 11. For example, 3 feet 14 inches would be adjusted to read 4 feet 2 inches.
setFeet This method accepts an int argument that is assigned to the feet field.
setInches This method accepts an int argument that is assigned to the inches field. The simplify method is then called.
getFeet This method returns the value in the feet field.
getInches This method returns the value in the inches field.
toString This method returns a string representing the distance held by the object. For example, if an object’s feet field holds 3 and its inches field holds 7, the toString method would return the string “3 feet 7 inches”.
add This method accepts a FeetInches object as its argument. It returns a reference to a FeetInches object that is the sum of the calling object and the object that was passed as the argument.
equals This method accepts a FeetInches object as its argument. It returns the boolean value true if the calling object and the argument object hold the same data. Otherwise it returns false.
copy This method returns a reference to a new FeetInches object that is a copy of the calling object.


 Code:


public class FeetInches
{
private int feet; // The number of feet
private int inches; // The number of inches
public FeetInches()
{
feet = 0;
inches = 0;
}
public FeetInches(int f, int i)
{
feet = f;
inches = i;
simplify();
}
public FeetInches(FeetInches object2)
{
feet = object2.feet;
inches = object2.inches;
}
private void simplify()
{
if (inches > 11)
{
feet = feet + (inches / 12);
inches = inches % 12;
}
}
public void setFeet(int f)
{
feet = f;
}
public void setInches(int i)
{
inches = i;
simplify();
}
public int getFeet()
{
return feet;
}
public int getInches()
{
return inches;
}
public String toString()
{
String str = feet + " feet " +  inches + " inches";
return str; }
public FeetInches add(FeetInches object2)
{
int totalFeet, // To hold the sum of feet
totalInches; // To hold the sum of inches
totalFeet = feet + object2.feet;
totalInches = inches + object2.inches;
return new FeetInches(totalFeet, totalInches);
}
public boolean equals(FeetInches object2)
{
boolean status;
if (object2 == null)
 status = false;
else if (feet == object2.feet &&  inches == object2.inches)
status = true;
else
status = false;
return status;
}
public FeetInches copy()
{
FeetInches newObject = new FeetInches(feet, inches);
return newObject; }

public static void main(String[] args) {

FeetInches newObject = new FeetInches(10,20);
System.out.println(newObject);

}

}


Monday, 24 September 2012

Televsion Revised Class

public
class Television {
private static String manufacturer = "Sony";
private static int ScreenSize = 40 ;
private static int channel = 1;
private static int volume = 0;
public static String getmanufacturer(){
return manufacturer;
}
public static void setmanufacturer (String Name){
manufacturer = "sony";
}
public static int getScreenSize(){
return ScreenSize;
}
public static void setScreenSize(int size){
ScreenSize = 40;
}
public static int getchannel(){
return channel;
}
public static void setchannel(int channel){
channel = 1;
}
public static int getvolume(){
return volume;
}
public static void setvolume(int volume){
volume = 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
setmanufacturer(
"Sony");
setScreenSize(40);
setchannel(1);
setvolume(0);
System.
out.println(getmanufacturer());
System.
out.println(getScreenSize());
System.
out.println(getchannel());
System.
out.println(getvolume());
}
}

Television Class

public

class Television {
private static String manufacturer = "Sony";
private static int ScreenSize = 40 ;
private static int channel = 1;
private static int volume = 0;
public static String getmanufacturer(){
return manufacturer;
}
public static void setmanufacturer (String Name){
manufacturer = "sony";
}
public static int getScreenSize(){
return ScreenSize;
}
public static void setScreenSize(int size){
ScreenSize = 40;
}
public static int getchannel(){
return channel;
}
public void setchannel(int channel){
channel = 1;
}
public static int getvolume(){
return volume;
}
public void setvolume(int volume){
volume = 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
setmanufacturer(
"Sony");
setScreenSize(40);
System.
out.println(getmanufacturer());
System.
out.println(getScreenSize());
}
}

Monday, 17 September 2012

Lab Assignments Codes Sep 17, 2012

Lab Assignments Codes
Q Muhammad Shahzad Khalil
Course InStructor: Ms Asma
Sep 17, 2012

Code 1 :

public class First {

            public static boolean A (int num){

                        for(int i=0; i>=num; i++){
                                   
                                    if(num % 4==0 && num % 100 != 0 || num % 4 == 0 && num % 100 == 0 && num % 400 == 0){
                                                return (true);
                                    }
                        }
                         return (false);
                       
            }

            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        System.out.println(A(16));

            }

}

Code 2:
public class armstrong {

public static void main(String[] args) {
for(int i=100;i<1000;i++){
int a=i%10;
int i1 = i/10;
int b = i1%10;
int i2 = i1/10;
int c = i2%10;
a=a*a*a;
b=b*b*b;
c=c*c*c;
int d=a+b+c;
if(d == i){
System.out.println(i);
}

}

}

}

Code 3 :
import java.util.Random;
public class randomNum{
public static void main Divisor (Int num){
if(num%2==0){
System.out.print(num +  “ is divisible by two”);
}                      
if(num%3==0){
System.out.print(num +  “ is divisible by three”);
}                      
if(num%5==0){
System.out.print(num +  “ is divisible by five”);
}                      
if(num%6==0){
System.out.print(num +  “ is divisible by six”);
}                      
if(num%10==0){
System.out.print(num +  “ is divisible by ten”);
}                      
if(num%15==0){
System.out.print(num +  “ is divisible by fifteen”);
}                      
if(num%30==0){
System.out.print(num +  “ is divisible by thirty”);
}                      

*/
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
Random rand= new Random();
int a= rand.nextInt();
Divisor(a);
}


Code 4 :
public static void month_checker (String month ) {
int n = 0;
String a = month.substring(0,3);
String out = a.toUpperCase();
System.out.println(out);
if (out.equals("JAN")) n=1;
if (out.equals("FEB")) n=2;
if (out.equals("MAR")) n=3;
if (out.equals("APR")) n=4;
if (out.equals("MAY")) n=5;
if (out.equals("JUN")) n=6;
if (out.equals("JUL")) n=7;
if (out.equals("AUG")) n=8;
if (out.equals("SEP")) n=9;
if (out.equals("OCT")) n=10;
if (out.equals("NOV")) n=11;
if (out.equals("DEC")) n=12;

if (n!=0) System.out.println("The number of the month is"+" "+n);
else System.out.println("This is not a valid month");
_______________________________________________________________________________

Code 5

import java.util.Scanner;
public class fifthques {
public static void main(String[] args) {
Scanner var5= new Scanner(System.in);
String name;
System.out.println("Enter ur name");
name = var5.next();

double phy;
double maths;
double eng;
double comp;
double ipl;
double percent;

System.out.println("Enter ur Physics Marks");
phy= var5.nextDouble();
System.out.println("Enter ur Maths Marks");
maths= var5.nextDouble();

System.out.println("Enter ur English Marks");
eng= var5.nextDouble();

System.out.println("Enter ur Computer Marks");
comp= var5.nextDouble();

System.out.println("Enter ur IPL Marks");
ipl= var5.nextDouble();

System.out.println("Your Name is " + name );
System.out.println("Your Physics Marks are " + phy);
System.out.println("Your Maths Marks are " + maths);
System.out.println("Your English Marks are " + eng);
System.out.println("Your Computer Marks are " + comp);
System.out.println("Your IPL Marks are " + ipl);
System.out.println("Your percentage is " +(percent=(((phy+maths+eng+comp+ipl)/500) *100) ));
}
}

Sunday, 16 September 2012

Stars triangle

public
class pattern_generator {
public static void Pehla(int input) {
for ( int i = 0; i<input ; i++ ) {
for (int j=0;j<input;j++) { System.out.print("*"); }
System.
out.println();}
}
public static void Doosra(int innput) {
for (int i = 0; i<innput;i++) {
for (int j=0;j<innput;j++) {
if (j<=i) {
System.
out.print("");} else {
System.
out.print("*");}
}
System.out
.println();}
}
public static void Teesra(int num) {
for (int i = 0; i<num;i++) {
for (int j=0;j<num;j++) {
if (j>=i) {
System.
out.print("");} else {
System.
out.print("*");}
}
System.
out.println();}
}
public static void Fourth(int n){
for(int i = 0;i <= n/2; i++)
{
for(int j = n/2-i;j>= 1;j--)
System.
out.print(" ");
for(int k=1;k<=2*(i+1)-1;k++)
System.
out.print('*');
System.
out.println();
}
}
public static void main(String[] args) {
Pehla(4);
System.
out.println();
Doosra(5);
Teesra(5);
System.
out.println();
Fourth(7);
}
}

Thursday, 13 September 2012

Stars

public
class Stars {
public static void main(int num){
for(int i=0; i<=num; i++){
for (int j=0 ; j<num ; j++){
System.
out.print("*");
}
System.
out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
main(4);
}
}