Solutions to Extra Exercises

1.      Write an application that declares three integer variables (a, b & c).  Set a to 15 and b to 6.  Add a & b and put the result into c.  Print out the result in c with a label saying that it is the sum.  Now take a mod b, and put the result into c.  Print out c, with a label identifying it as the mod.

 

import java.io.*;

 

public class MoreEx1

{

            public static void main ( String argv[])

            {

                        int a,b,c;

                        a=15;

                        b=6;

                        c= a +b;

                        System.out.println("The sum of a and b is " + c );

                        c= a% b;

                        System.out.println("a mod b is " + c  );

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.      Declare float variables to hold 17 and 3.  Declare a string variable to hold the string “The answer is:  “.  Print out the result of dividing 17 by 3, labeled with the string (“The answer is 5.666666”).

 

import java.io.*;

 

public class MoreEx2

{

            public static void main ( String argv[])

            {

                        float a,b;

                        a=17;

                        b=3;

                        String result = "The answer is ";

                        System.out.println(result + (a / b));

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.      Declare and initialize variables for your first name, last name, middle initial, and age.  (The contents of the last variable can be made up, if you want!)  Using your variables, print out your name – last name first, comma, first name.  Skip a line.  Then print out your age on another line.

 

import java.io.*;

 

public class MoreEx3

{

            public static void main ( String argv[])

            {

                        String fname="Bob", lname="Hartley", mname="I";

                        int age=45;

                        System.out.println(lname + ", " + fname+"\n");

                        System.out.println(age);

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.      Print the line “This is a loop test” on the screen 3 times, on three separate lines.

 

import java.io.*;

 

public class MoreEx4

{

            public static void main ( String argv[])

            {

                        for (int x = 1; x <=3; x++ )

                                    System.out.println("This is a loop test.");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.      Print the line “This is a loop test” on the screen 3 times, with a space between each line.

 

import java.io.*;

 

public class MoreEx5

{

            public static void main ( String argv[])

            {

                        for (int x = 1; x <=3; x++ )

                                    System.out.println("This is a loop test.\n");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.      Print on the screen (using a loop, of course), this:

This is line 1.

This is line 2.

This is line 3.

 

import java.io.*;

 

public class MoreEx6

{

      public static void main ( String argv[])

      {

            for (int x = 1; x <=3; x++ )

                        System.out.println("This is line "+x+".");

      }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Print on the screen these lines:

46. This is a line.

47. This is a line.

48. This is a line.

 

import java.io.*;

 

public class MoreEx7

{

            public static void main ( String argv[])

            {

                        for (int x = 46; x <=48; x++ )

                                    System.out.println(x +". "+"This is a line.");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Using a conditional statement, check whether a number in a variable is even or odd.  Print out either “even” or “odd”.  Try several numbers in the variable, to be sure your program is working correctly. HINT:  You can tell whether a number is even or odd by checking its modulus 2.

 

import java.io.*;

 

public class MoreEx8

{

            public static void main ( String argv[])

            {

                        int x = 42;

                        if ( x % 2 == 0 )

                                    System.out.println("Even");

                        else

                                    System.out.println("Odd");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9.      Declare an integer variable.  Then use a conditional to do the following:  If the variable is 2, print “It’s a 2.”  If the variable is 3, print “It’s a 3.”  Otherwise, print “It’s something else.”   Now try putting 2, 3 and 5 into the variable.  Did it work right for 2?  (It should print only “It’s a 2”, and not “It’s something else.”)  If not, it is because of the way you have nested your “if’s”.

 

import java.io.*;

 

public class MoreEx9

{

            public static void main ( String argv[])

            {

                        int x = 2;

                        if ( x == 2 )

                                    System.out.println("It's a 2.");

                        else if ( x == 3 )

                                    System.out.println("It's a 3 ");

                        else

                                    System.out.println("It's something else.");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Solutions to Extra Exercises

1.      Write an application that declares three integer variables (a, b & c).  Set a to 15 and b to 6.  Add a & b and put the result into c.  Print out the result in c with a label saying that it is the sum.  Now take a mod b, and put the result into c.  Print out c, with a label identifying it as the mod.

 

import java.io.*;

 

public class MoreEx1

{

            public static void main ( String argv[])

            {

                        int a,b,c;

                        a=15;

                        b=6;

                        c= a +b;

                        System.out.println("The sum of a and b is " + c );

                        c= a% b;

                        System.out.println("a mod b is " + c  );

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.      Declare float variables to hold 17 and 3.  Declare a string variable to hold the string “The answer is:  “.  Print out the result of dividing 17 by 3, labeled with the string (“The answer is 5.666666”).

 

import java.io.*;

 

public class MoreEx2

{

            public static void main ( String argv[])

            {

                        float a,b;

                        a=17;

                        b=3;

                        String result = "The answer is ";

                        System.out.println(result + (a / b));

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.      Declare and initialize variables for your first name, last name, middle initial, and age.  (The contents of the last variable can be made up, if you want!)  Using your variables, print out your name – last name first, comma, first name.  Skip a line.  Then print out your age on another line.

 

import java.io.*;

 

public class MoreEx3

{

            public static void main ( String argv[])

            {

                        String fname="Bob", lname="Hartley", mname="I";

                        int age=45;

                        System.out.println(lname + ", " + fname+"\n");

                        System.out.println(age);

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.      Print the line “This is a loop test” on the screen 3 times, on three separate lines.

 

import java.io.*;

 

public class MoreEx4

{

            public static void main ( String argv[])

            {

                        for (int x = 1; x <=3; x++ )

                                    System.out.println("This is a loop test.");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.      Print the line “This is a loop test” on the screen 3 times, with a space between each line.

 

import java.io.*;

 

public class MoreEx5

{

            public static void main ( String argv[])

            {

                        for (int x = 1; x <=3; x++ )

                                    System.out.println("This is a loop test.\n");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.      Print on the screen (using a loop, of course), this:

This is line 1.

This is line 2.

This is line 3.

 

import java.io.*;

 

public class MoreEx6

{

      public static void main ( String argv[])

      {

            for (int x = 1; x <=3; x++ )

                        System.out.println("This is line "+x+".");

      }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Print on the screen these lines:

46. This is a line.

47. This is a line.

48. This is a line.

 

import java.io.*;

 

public class MoreEx7

{

            public static void main ( String argv[])

            {

                        for (int x = 46; x <=48; x++ )

                                    System.out.println(x +". "+"This is a line.");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Using a conditional statement, check whether a number in a variable is even or odd.  Print out either “even” or “odd”.  Try several numbers in the variable, to be sure your program is working correctly. HINT:  You can tell whether a number is even or odd by checking its modulus 2.

 

import java.io.*;

 

public class MoreEx8

{

            public static void main ( String argv[])

            {

                        int x = 42;

                        if ( x % 2 == 0 )

                                    System.out.println("Even");

                        else

                                    System.out.println("Odd");

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9.      Declare an integer variable.  Then use a conditional to do the following:  If the variable is 2, print “It’s a 2.”  If the variable is 3, print “It’s a 3.”  Otherwise, print “It’s something else.”   Now try putting 2, 3 and 5 into the variable.  Did it work right for 2?  (It should print only “It’s a 2”, and not “It’s something else.”)  If not, it is because of the way you have nested your “if’s”.

 

import java.io.*;

 

public class MoreEx9

{

            public static void main ( String argv[])

            {

                        int x = 2;

                        if ( x == 2 )

                                    System.out.println("It's a 2.");

                        else if ( x == 3 )

                                    System.out.println("It's a 3 ");

                        else

                                    System.out.println("It's something else.");

            }

}