Friday, August 2, 2013

Lab Exercises

1.Class Name: DegreeConverter.

import java.util.*;

class DegreeConverter {
  public static void main(String[] args) {
    float temperature;
    double c,f;
   
    Scanner in = new Scanner(System.in);     

    System.out.println("Enter temperatue in Fahrenheit");
    f = in.nextDouble();

   c = ( f-32)/1.8;

    System.out.println("Temperature in Celsius = " + c);
  }
}



============================================================
2. Class CurrencyConverter


import java.util.*;
public class CurrencyConverter {

   
   
    public static void main(String[] args) {
        double USD=43.75, GBP=78.98, EUR=56.45, JPY=0.867;
        double USDmoney=0,GBPmoney=0,EURmoney=0,JPYmoney=0;
        int Pmoney;
        Scanner in = new Scanner(System.in); 
       
        System.out.println("Enter currency:");
        Pmoney = in.nextInt();
       
       
        USDmoney = Pmoney* USD;
        GBPmoney = Pmoney * GBP;
        EURmoney = Pmoney * EUR;
        JPYmoney = Pmoney* JPY;
       
       System.out.println("Peso to USD:" + USDmoney + "\n Peso to GBP:" + GBP + "\nPeso to EUR:" +
       EURmoney + "\n Peso to JPY:" + JPYmoney);
    }

}


 ============================================================
3. Class Name: Pay.

 ============================================================
4. Class Name: MileAge
 ============================================================

5.Class Name: Quadratic

import java.util.*;
public class Quadratic {
   
    public static void main(String[] args) {
       
        Scanner console = new Scanner (System.in);

        double a , b , c , d , r , rr, sq ;

        System.out.println("Please enter the value of a:");
        a = console.nextDouble();

        System.out.println("Please enter the value of b:");
        b = console.nextDouble();

        System.out.println("Please enter the value of c:");
        c = console.nextDouble();

        d = ((b * 2) - 4) * (a * c);
        r = -b / (2 * a);
        rr=(-b+d) / (2*a);
        System.out.println("The Quadratic answer is: " + d);

        if ( d < 0){
        System.out.println("No root available");}

        else if ( d == 0){
        System.out.println("Roots are equal");
        System.out.println("The answer for the roots is: " + r);}

        else if (d > 0){

        System.out.println("The Displayed answer is:" + rr );}

        else
        System.out.println("Invalid");
        System.exit(0);


        }

        }
   


 ============================================================
6. Class EvenNum.






public class EvenNum {

    public static void main(String[] args)
    {
       
       
        //FOR LOOP
        System.out.println("------------ using for loop ------------");
        for (int a=2; a<=50; a++)
        {
            System.out.print(a+" ");       
            a=a+1;
        };
       
       
        //WHILE LOOP
        System.out.println(" ");
        System.out.println("------------ using while loop ------------");
        int a = 2;
        while(a<=50)
        {
            System.out.print(a+" ");
            a=a+2;
        };
       
       
        //DO..WHILE LOOP
        System.out.println(" ");
        System.out.println("------------ using do...while loop ------------");
       
        int x = 2;
        do
        {
            System.out.print(x+" ");
            x=x+2;
        }
        while(x<=50);
       

    }

}



 ============================================================

7. Class Sumof50


public class Sumof50 {

    public static void main(String[] args) {
      
        int sum = 0;
      
        for (int i=1; i<=50; i++)
        {      
            sum = i+sum;
          
        };
      
      
        System.out.println("Sum numbers from 1-50: " + sum);

    }

}


 ============================================================
8. Class name is TableOfSquares.


public class TableOfSquares {

    public static void main(String[] args) {
       
        //for loop
       
        System.out.println("----- Using for loop -----");
        for(int a=1; a<=20;a++)
        {
            System.out.println();
            System.out.print("Number: " + a + "\tSquare: " + (a*a) );
        };
        System.out.println(" " + "\n" + "\n");
       
       
        //WHILE LOOP
        int x=1;
        System.out.println("----- Using while loop -----");
        while(x<=20)
        {
            System.out.println();
            System.out.print("Number: " + x + "\tSquare: " + (x*x) );
            x++;
        };
        System.out.println(" " + "\n" + "\n");
       
       
       
        //DO..WHILE LOOP
        int a=1;
        System.out.println("----- Using do..while loop -----");
        do{System.out.println();
        System.out.print("Number: " + a + "\tSquare: " + (a*a) );
        a++;}

        while (a<=20);
       
       
       
       

    }

}


 ============================================================
9. Class Name: NameValidation

import java.util.Scanner;
public class NameValidation {
   
    public static void main(String[] args) {
      
        Scanner s= new Scanner(System.in);
        String word;
        int lastletter;
      
        do{
            System.out.print("Enter a word: ");
            word = s.next();
            lastletter = word.length()-1;
            if (word.equals("exit")){
            System.out.println("Program is now terminating…");
            break;
            }
            else if (word.charAt(lastletter)==word.charAt(0))
            System.out.println("The first character is EQUAL to its last character: "+word);
            else
            System.out.println("The first character is NOT EQUAL to its last character: "+word);

            }
            while (word != null);

            }

            }
      

 ============================================================
10. Class Name: Prime

 ============================================================

*************************JAVA BASICS : Using JOptionPane Class*****************

 ============================================================

11. Class Name MonthBalance

import javax.swing.JOptionPane;
public class MonthBalance {

   
    public static void main(String[] args) {
        String choice="element";
        String Menu="\nd-Deposit \nw-Withdrawal \ne-Exit";
        
           String Choice="element";
        double balance = 0,d=0;
        double yearlyRate=0.025,w=0;
      
  

  
   do{
       choice=JOptionPane.showInputDialog(Menu+"\n Enter Choice:");
       choice=choice.toLowerCase();
      
       switch(choice.charAt(0) ){
      
       case 'd':
         
     JOptionPane.showMessageDialog(null,"Beginning of Month Balance:" + balance);
    balance=Double.parseDouble(JOptionPane.showInputDialog("enter Deposit"));
     JOptionPane.showMessageDialog(null,"Beginning of Month Balance:" + balance);
   
     break;
    
       case 'w':
         
       JOptionPane.showMessageDialog(null,"Beginning of Month Balance:"+balance);
       w=Double.parseDouble(JOptionPane.showInputDialog("Enter withdrawal amount:"));
       balance = balance+(yearlyRate / 12) * balance;
       JOptionPane.showMessageDialog(null,"Balance after transaction:"+ (balance=balance-w));
     
       break;
      
       case 'e': System.exit(0);
      
       break;
      
       default:JOptionPane.showMessageDialog(null,"Access Denied!");
       }
       }while(choice.charAt(0)!=('e'));
       }
  

    }



 ============================================================

12.  Class Name CharacterorString

import java.util.*;
import javax.swing.JOptionPane;

import javax.swing.JOptionPane;

public class CharacterorString{
public static void main(String[] Theory)
{
    String SentenceContents=JOptionPane.showInputDialog("Enter a string:" );


    int VowelCount = 0,ConsonantCount = 0,WordCount = 0,SpaceCount = 0,SpecialCharCount = 0 ;

    for (int Bat = 0; Bat < SentenceContents.length(); Bat++)
    {
        char Vowels = SentenceContents.charAt(Bat );
        if ( (Vowels == 'A') || (Vowels == 'a' )
        || (Vowels == 'E') || (Vowels == 'e' )
        || (Vowels == 'I') || (Vowels == 'i' )
        || (Vowels == 'O') || (Vowels == 'o' )
        || (Vowels == 'U') || (Vowels == 'u' ))
        VowelCount++;

        char Consonants = SentenceContents.charAt(Bat);
        if ( (Consonants == 'B') || (Consonants == 'b')
        || (Consonants == 'C') || (Consonants == 'c')
        || (Consonants == 'D') || (Consonants == 'd')
        || (Consonants == 'F') || (Consonants == 'f')
        || (Consonants == 'G') || (Consonants == 'g')
        || (Consonants == 'H') || (Consonants == 'h')
        || (Consonants == 'J') || (Consonants == 'j')
        || (Consonants == 'K') || (Consonants == 'k')
        || (Consonants == 'L') || (Consonants == 'l')
        || (Consonants == 'M') || (Consonants == 'm')
        || (Consonants == 'N') || (Consonants == 'n')
        || (Consonants == 'P') || (Consonants == 'p')
        || (Consonants == 'Q') || (Consonants == 'q')
        || (Consonants == 'R') || (Consonants == 'r')
        || (Consonants == 'S') || (Consonants == 's')
        || (Consonants == 'T') || (Consonants == 't')
        || (Consonants == 'V') || (Consonants == 'v')
        || (Consonants == 'W') || (Consonants == 'w')
        || (Consonants == 'X') || (Consonants == 'x')
        || (Consonants == 'Y') || (Consonants == 'y')
        || (Consonants == 'Z') || (Consonants == 'z') )

        ConsonantCount++;

        char Spaces = SentenceContents.charAt(Bat);
        if ( (Spaces == ' ') )

        SpaceCount++;

        char SpecialCharacters = SentenceContents.charAt(Bat);
        if ( (SpecialCharacters == '!') || (SpecialCharacters == '@' )
        || (SpecialCharacters == '#') || (SpecialCharacters == '$' )
        || (SpecialCharacters == '%') || (SpecialCharacters == '^' )
        || (SpecialCharacters == '&') || (SpecialCharacters == '*' )
        || (SpecialCharacters == '(') || (SpecialCharacters == ')' )
        || (SpecialCharacters == '-') || (SpecialCharacters == '_' )
        || (SpecialCharacters == '+') || (SpecialCharacters == '=' )
        || (SpecialCharacters == ',') || (SpecialCharacters == '<' )
        || (SpecialCharacters == '.') || (SpecialCharacters == '>' )
        || (SpecialCharacters == '?') || (SpecialCharacters == '/' )
        || (SpecialCharacters == '"') || (SpecialCharacters == ';' )
        || (SpecialCharacters == ':') || (SpecialCharacters == '{' )
        || (SpecialCharacters == '[') || (SpecialCharacters == '}' )
        || (SpecialCharacters == ']') || (SpecialCharacters == '~' )
        || (SpecialCharacters == '`') || ((SpecialCharacters == '1' )
        || (SpecialCharacters == '2') || (SpecialCharacters == '3' )
        || (SpecialCharacters == '4') || (SpecialCharacters == '5' )
        || (SpecialCharacters == '6') || (SpecialCharacters == '7' )
        || (SpecialCharacters == '8') || (SpecialCharacters == '9' )
        || (SpecialCharacters == '|') ))

        SpecialCharCount++;
        String done="";
        char Terminate = SentenceContents.charAt(Bat);
        if (Terminate == 'd')
        JOptionPane.showMessageDialog(null,"the System is terminating");
        if ( SentenceContents != null)
   
    {
    }
    }
    JOptionPane.showMessageDialog(null,
                             "There are " + VowelCount + " vowels in this sentence\n"+
                             "There are " + ConsonantCount + " consonants in this sentence\n"+
                             "There are " + SpaceCount + " spaces in this sentence\n" +
                             "There are " + SpecialCharCount + " special characters in this sentence" );


    }

    }
       
   
 ============================================================
13.
 ============================================================
14.
 ============================================================
15.
 ============================================================
16. Class Name Reverse

import java.io.*;
public class Reverse {

   
    public static void main(String[] args)throws Exception
    {
        BufferedReader fb=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("enter string");
        String name=fb.readLine();
      
        String reverse=new StringBuffer(name).reverse().toString();
      
        System.out.print("the reverse string is:"+reverse);

    }

}


 ============================================================
17.













No comments:

Post a Comment