Starting from:

$30

COMP2120-Assignment 1 Solved

Problem 1.      
            First update the pseudocode for the account with interest rate, and then write a Java program for that to print the balance of an account after the first, second and third year, if the initial balance is $1,000 and earns 5% interest per year. Complete your code by putting a general document about your program at the top of the code as well as documents for the variables and statements inside your program.

 

 

Problem 2.     

The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a/2. Then find the average of your guess g and a/g. That’s your next guess. Repeat until two consecutive guesses are close enough, e.g. less than a threshold t. Write pseudocode for this algorithm.

             

 

Problem 3.      
            Write a Java program to compute and print the sum and multiplication of the first five negative integers, starting from -1, -2, …. Add document to your code.

 

 

Problem 4.      

            Type in the following program. Then, using the Jave API Documentation on Internet, try to complete the first two lines of the code. Then try to run the program and fix any compile-time errors you find in it. After the successful run, modify it to show a different greeting and image.

 

 

            import java.net. ...;   import javax. ...;

            import javax.swing.ImageIcon;

 

      public Class Test 

      

                    public Static main(String[] args) throws Exception {

                URL imageLocation = new URL(

                  “http://horstmann.com/java4everyone/duke.gif”);

                   JOptionPane.showMessageDialog(null, “Hello”, “Title”,

                                 JOptionPane.PLAIN_MESSAGE, new ImageIcon(imageLocation))

           

 

      }

                                                                                                             1        

Problem 5.      

 Write a Java program, in which declare and initialize required variables and compute the following mathematical operations:

 

𝑔𝑡

                                   𝑠 =                 𝑠$ +         𝑣$ +          ’(                 (                                                                     𝐺 = 4𝜋(         01 2.3/421

 

Problem 6.      

            What are the values of the following expressions? In each line assume that

             

  double x = 2.5;  double y = -1.5;     int n = 17;     int m = 18;

      

a.    x + n * y – (x + n) * y

b.    m / n + m % n

c.    n % 2 + m % 2

d.    (m + n) / 2

e.    (m + n) / 2.0;

f.    (int) (0.5 * (m + n)

g.    1 - (1 - (1 - n)

h.    n % 10 + (n – (n % 10))

 

More products