COMPUTER PROGRAMMING 2014-2015 WEEK 1 PROBLEMS

As a homework the code plus results of the exercises will be copied into Word, printed out with your Name and student number and will be bring to the class as homework next week, some of the exercise problems will be asked also as quiz problems. Quiz+homework will be 30 % of the class grade. Another midterm exam will be 30 % and final will be 40% of the total.

 

 

Q1)  Write the following code into the editor, save it as welcome.java and run it by using java command

class welcome

{

public static void main(String args[])

 {

 System.out.println("Welcome to Java Programming!");

 }

 }

 

Q2)  Write the following code into the editor and run it by using java command

import javax.swing.*;// program uses class JOptionPane in swing library

public class BPH1Q2

{

     // main method begins execution of Java application

     public static void main( String args[] )

     {

     JOptionPane.showMessageDialog(null,"Welcome to Java Programming!" );

     } // end method main

 

} // end class BPIH1E3

 

Q3)  Write the following code into the editor and run it by using java command

import javax.swing.JOptionPane;

class BPH1Q3

{    public static void main (String args[])

     {

     String name=JOptionPane.showInputDialog("Please enter your name :");

     JOptionPane.showMessageDialog(null,"Welcome to java class! "+name,

     "Welcome",JOptionPane.INFORMATION_MESSAGE);

     }

}

 

Q4)  Write the following code into the editor and run it by using java command

import javax.swing.*;// program uses class JOptionPane in swing library

public class BPH1Q4

{

     // main method begins execution of Java application

     public static void main( String args[] )

     {

     int number1=35;

     int number2=28;

     int sum=number1+number2;

     String s="sum = "+sum;

     JOptionPane.showMessageDialog(null,s );

     } // end method main

 

} // end class BPH1Q4

 

Q5) Write the following code in the editor and run it

import javax.swing.*;// program uses class JOptionPane in swing library

 

class BPH1Q5

 {    // main method begins execution of Java application

      public static void main(String args[])

      {

          int number1,number2;

          int sum,substract,multiply,divide,remain;

          number1=2;

          number2=3;

          sum = number1+number2;

          substract = number1-number2;

          multiply = number1*number2;

          divide=number1/number2;

          remain=number1%number2;

          String s=number1+" +  "+number2+" = "+sum+"\n";

          s+=number1+" -  "+number2+" = "+substract+"\n";

          s+=number1+" x  "+number2+" = "+multiply+"\n";

          s+=number1+" /  "+number2+" = "+divide+"\n";

          s+=number1+" %  "+number2+" = "+remain+"\n";

          JOptionPane.showMessageDialog(null,s);

       }// end method main

 }

 

Change the program so that you will read two integer from screen input by using

     number1 = Integer.parseInt(JOptionPane.showInputDialog("enter first integer:"));

The name of the program and class will be BPH1Q5A.java

 

Q6)  Write the following code into the editor and run it by using java command

 import javax.swing.*;// program uses class JOptionPane in swing library

 

public class BPH1Q6

{

     // main method begins execution of Java application

     public static void main( String args[] )

     {         

        int number1; // first number to add  

        int number2; // second number to add 

        int sum; // sum of number1 and number2

        number1=Integer.parseInt(JOptionPane.showInputDialog("Enter first integer: "));

        number2=Integer.parseInt(JOptionPane.showInputDialog("Enter second integer: "));

        sum = number1 + number2; // add numbers

        JOptionPane.showMessageDialog(null,"Sum is "+sum ); // display sum

     } // end method main

 

} // end class BPH1Q6

 

Q7)  Write the following code in the editor and run it

 import javax.swing.*;

 

public class BPH1Q7

{

     // main method

     public static void main( String args[] )

     {         

        double number1; // first integer  

        double number2; // second integer

        double sum; //

        number1 = Double.parseDouble(JOptionPane.showInputDialog("enter first integer:"));

        number2 = Double.parseDouble(JOptionPane.showInputDialog("enter se:"));

        sum = number1 + number2; // add numbers

        JOptionPane.showMessageDialog(null,"Total ="+sum ); // Show sum

     } // end of main method

 

} //  end of class

 

Write another program to read an integer number, n, calculate f(n)=n*n+2*n-5 and shows the result. The name of the program and class will be BPIH1Q7.java

Q8) Write a program to enter a double variable named x in  graphic screen and write the results of x and y(x)=x*x+2*x-5  in graphic screen. The name of the program and class will be BPH1Q8.java

Q9) Write a program to enter double variables named x and y in  graphic screen and write the results of z(x,y)=x*x+y*y in graphic screen. The name of the program and class will be BPH1Q9.java

Q10) Write a program toprint out the following String statement: String s="*\n *\n  *\n   *\n    *";   in graphic screen