Lab Announcements


        News     Lecture Notes     Lab Assignments     Lab News     Solved Problems     Calendar


exam

posted Apr 16, 2014, 2:11 AM by Dlovan ismail   [ updated Apr 16, 2014, 2:12 AM ]

/*

Write a program using switch case to do the following:

Note: You must enter a number to represent the case of the switch

1. write a program which reads string from the JOptionPane then put it in a text file (computer.txt)
2. Write a function named (search) that take one argument (type string) represent path of the file and to do counts the number of times the character ’e’ appears in the text file then  prints that number on the screen.
Note: if the character ’e’ not appears in the text file must be print "the character ‘e’ is not found"
3. Write a function named (Sum_Prime) which prints 10 prime numbers randomly between (5-100) and print their total.
*/

 

package exam2;

import java.io.*;

import java.util.*;

import javax.swing.JOptionPane;

 

public class Exam2

{

 

    public static void main(String[] args)throws IOException

    {

        System.out.print("Please choose one of them 1,2 or 3 : ");

        Scanner ch=new Scanner(System.in);

        switch(ch.nextInt())

        {

            case 1:PrintWriter output =new PrintWriter("computer.txt");

                output.println(JOptionPane.showInputDialog("Enter your String :"));

                output.close();

                break;

            case 2:Search("d:computer.txt");

                break;

            case 3:Sum_Prime();

                break;

            default :System.out.println("invalid number");

        }

    }

   

   static void Search(String Path)throws IOException

   {

       Scanner input=new Scanner(new File(Path));

       int count=0;

       while(input.hasNextLine())

       {

           String str=input.nextLine();

           for (int i = 0; i <str.length(); i++)

               if(str.charAt(i)=='e')

                   count++;

       }

      

       if(count!=0)

           System.out.println("the character ’e’ appears in the text file "+count+" times");

       else

           System.out.println("the character ‘e’ is not found");

   }

  

   static void Sum_Prime()

   {

       int count=1,sum=0;

       Random rand=new Random();

      

       while(count<=10)

       {

           int p=rand.nextInt(96)+5;

           boolean flag=true;

           for (int i = 2; i <p; i++)

               if(p%i==0)

                   flag=false;

          if(flag)

          {

              System.out.println(p);

              sum+=p;                  

              count++;

          }

       }

      

       System.out.println("Sum all prime numbers="+sum);

   }

}

6-2 StudentTest

posted Apr 10, 2014, 4:05 AM by Dlovan ismail   [ updated Apr 10, 2014, 8:55 AM ]


package studenttest;

public class Student 
{
        private String name,address;
        private int age;

        void Set_Name(String n)
        {
            name=n;
        }

        void Set_Address(String s)
        {
            address=s;
        }

        void Set_Age(int a)
        {
            age=a;
        }

        String Get_Name()
        {
            return name;
        }

        String Get_Address()
        {
            return address;
        }

        int Get_Age()
        {
            return age;
        }
}



package studenttest;

public class StudentTest 
{

    public static void main(String[] args) 
    {
           Student std1=new Student();
           Student std2=new Student();

           std1.Set_Name("ali");
           std1.Set_Address("duhok");
           std1.Set_Age(18);

           std2.Set_Name("salem");
           std2.Set_Address("zakho");
           std2.Set_Age(25);

           System.out.println("The information about std1 :");
           System.out.println("The name : "+std1.Get_Name());
           System.out.println("The address : "+std1.Get_Address());
           System.out.println("The age : "+std1.Get_Age());

           System.out.println("\n\nThe information about std2 :");
           System.out.println("The name : "+std2.Get_Name());
           System.out.println("The address : "+std2.Get_Address());
           System.out.println("The age : "+std2.Get_Age());
       
    }
    
}

6-1 Circle_Object

posted Apr 9, 2014, 2:07 PM by Dlovan ismail



package circle_object;

public class Circle 
{
       private double radius; 

       void Set_Radius(double r)
       {
          radius=r; 
       }

       double get_Radius()
       {
          return radius; 
       }

       double Display_Area()
       {
          return Math.pow(radius,2)*Math.PI; 
       }
}




package circle_object;

public class Circle_Object 
{

    public static void main(String[] args) 
    {

            Circle box=new Circle();  // Create a Circle object. 
            box.Set_Radius(4);
            System.out.println("The area of the circle of radius " + box.get_Radius()+" is " + box.Display_Area());

    }
    
}


5-10 Prime_Display

posted Apr 7, 2014, 7:11 AM by Dlovan ismail


package prime_display;
import java.util.Scanner;

public class Prime_Display 
{

        public static void main(String[] args) 
        {
            Scanner input=new Scanner(System.in);
            
            System.out.print("Please choose  one of them 1 , 2 : ");
            int number=input.nextInt();
            
            switch(number)
            {
                case 1:Display1(); break;
                case 2:Display2(); break;
                default: System.out.println("invalid number");
            }

        }

        public static void Display1() 
        {
            for (int i = 2; i <=100; i++) 
            {
                boolean flag=true;

                for (int j = 2; j <i; j++) 
                    if(i%j==0)
                        flag=false;

                if(flag&&i%10>=5)
                    System.out.print(i+" ");

            }

            System.out.println();
        }

        public static void Display2() 
        {
            for (int i = 2; i <=100; i++) 
            {
                boolean flag=true;

                for (int j = 2; j <i; j++) 
                    if(i%j==0)
                        flag=false;

                if(flag&&i%10<5)
                    System.out.print(i+" ");

            }

            System.out.println();
        }
   
}

5-9 Average_Positive

posted Apr 7, 2014, 4:06 AM by dldar masud   [ updated Apr 7, 2014, 4:13 AM ]

/*
 Write a function called average that reads positive integers 
 from the user, stopping when the user enters a 0 or negative integer 
 and return the average of the positive integers then print it in main function.
*/

package average_positive;
import java.util.Scanner;

public class Average_Positive 
{

        public static void main(String[] args) 
        {
           double index=average();

            if(index!=-1)
                System.out.println("Average="+index);
            else
              System.out.println("No average"); 
        }

        public static double average()
        {
           Scanner input=new Scanner(System.in);

           System.out.println("Enter the numbers :");
           double sum=0,count=0,number;

           while((number=input.nextDouble())>0)
           {
               sum+=number;
               count++;
           }

           return(count!=0)?sum/count:-1;

        }
}

..........................................................................................
run:
Enter the numbers :
44
66
75
43
0
Average=57.0
BUILD SUCCESSFUL (total time: 18 seconds)

5-8 Word

posted Apr 6, 2014, 9:39 AM by Dlovan ismail

/*
Write a program WordCounter that calls a function that accepts a string (from main) and 
returns the number of words contained in the string. For instance, if the string argument is “I love 
java Programming” the function should return the number 4. 

*/

package word;
import java.util.Scanner;

public class Word 
{

        public static void main(String[] args) 
        {
            Scanner scan=new Scanner(System.in);

            System.out.print("Enter your string : ");
            System.out.println("Number of words contained in the string : "+WordCounter(scan.nextLine()));

        }

        public static int WordCounter(String s) 
        {
            Scanner input=new Scanner(s);
            int count=0;

            while(input.hasNext())
            {
                count++;
                input.next();
            }

            return count;
        }
    
}
.............................................................................................
run:
Enter your string : i love java programming
Number of words contained in the string : 4
BUILD SUCCESSFUL (total time: 11 seconds)



5-7 Method_F

posted Apr 4, 2014, 9:01 AM by Dlovan ismail   [ updated Apr 4, 2014, 9:27 AM ]



package method_f;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;

public class Method_F 
{
        public static void main(String[] args)throws IOException 
        {
           Scanner scan=new Scanner(System.in);

           System.out.print("Enter your file path : ");
           OutputFile(scan.next());
        }

        public static void OutputFile(String s)throws IOException
        {
            PrintWriter p=new PrintWriter(s);

            p.println("Names\t\tAverage");
            p.println("-------\t\t--------");

            Scanner scan=new Scanner(System.in);

            int pass=0,fail=0;

           System.out.print("How many students who wish to add their names and averages in the file : ");

            for (int i =scan.nextInt(); i>=1; i--)
            {
                String name=get_name();
                double average=get_average();

                if(average>=50)
                    pass++;
                else
                    fail++;

                p.println(name+"\t\t"+average);
 
            }

            p.println("number of student passed = "+pass);
            p.println("number of student failed = "+fail);

            p.close();

        }

        public static String get_name()
        {
            return JOptionPane.showInputDialog("Enter name of student :");
        }

        public static double get_average()
        {
            return Double.parseDouble(JOptionPane.showInputDialog("Enter average of student :"));
        }
    
}

5-6 Prime

posted Apr 4, 2014, 6:12 AM by Dlovan ismail   [ updated Apr 4, 2014, 6:12 AM ]

/* Write a function that has one argument n ( n is an  integer)
and return true if n is a prime number otherwise return false */


package prime;
import java.util.Scanner;

public class Prime
{
        public static void main(String[] args) 
        {
            Scanner scan=new Scanner(System.in);

            System.out.print("Enter the number : ");
            int n=scan.nextInt();

            if(IsPrime(n))
                System.out.println(n+" is a prime number");
            else
                System.out.println(n+" is not a prime number");

        }

        public static boolean IsPrime(int n)
        {
            boolean flag=true;

            for (int i =2; i < n; i++) 
                if(n%i==0)
                    flag=false;

            return(flag&&n>1);

        }
    
}

5-5 Location

posted Apr 3, 2014, 4:06 AM by Dlovan ismail   [ updated Apr 7, 2014, 7:30 AM ]

//Write a program to find location of given word in a sentence using function

package location;
import java.util.Scanner;

public class Location 
{
        public static void main(String[] args) 
        {
            Scanner scan=new Scanner(System.in);

            System.out.print("Enter a sentence : ");
            String s=scan.nextLine();

            System.out.print("Enter a word : ");
            String w=scan.next();

            int index=Find(s, w);

            if(index!=-1)  
            System.out.println("Location of given word ="+index);
            else
            System.out.println("The word is not found in a sentence");  

        }  

        public static int Find(String s,String w)
        {
            Scanner scan=new Scanner(s);
            int L=0,flag=0;

            while(scan.hasNext())
            {
                L++;
                
                if(scan.next().compareTo(w)==0)
                    {
                      flag=1;
                      break;
                    }

            }

            return(flag==1)?L:-1;

        }
}

5-4 MaxMin

posted Apr 3, 2014, 3:26 AM by Dlovan ismail   [ updated Apr 3, 2014, 3:28 AM ]

/*Write a program containing two functions that return values. The first Function returns
the higher of two numbers entered by the user. The second Function returns the lowest
number. The parameters for the two functions are entered through the main function.*/

package maxmin;
import java.util.Scanner;

public class MaxMin 
{

        public static void main(String[] args) 
        {
            Scanner Scan=new Scanner(System.in);

            System.out.print("Enter the number1 : ");
            double n1=Scan.nextDouble();

            System.out.print("Enter the number2 : ");
            double n2=Scan.nextDouble();

            System.out.println("Max="+Max(n1,n2));
            System.out.println("Min="+Min(n1,n2));

        }

        public static double Max(double a,double b)
        {
            return(a>b)?a:b;
        }

        public static double Min(double a,double b)
        {
            return(b<a)?b:a;
        }
    
}

1-10 of 37