Programming Languages‎ > ‎Java‎ > ‎

Java Programming Assignment 1

Requirement

INCLUDE IN EACH PROGRAM (if more than one problem in one assignment, each program for each problem should have the following) comments indicating:
  • Name of program
  • Programmer's name
  • Current Date
  • Computer system and compiler you are using
  • Brief description of the program (1-5 sentences)
  • Variable names (if any) and descriptions of what they represent
Problem A
Write a Java application program to print (display) information shown below. BE SURE TO LINE UP YOUR INFORMATION LIKE BELOW, INCLUDING PUTTING NEWLINES AS SHOWN BELOW! You don't need any variables in this program.

Student Learning Outcomes
Outcome 1:
Read, analyze and explain intermediate level Java
programs.
Outcome 2:
Design solutions for intermediate level problems using
appropriate design methodology incorporating objectoriented
intermediate programming constructs.
Outcome 3:
Create algorithms, code, document, debug, and test
intermediate level Java programs.

Problem B
Write another Java application program which initializes an int variable to 0x468A does the following in a loop 3 times:
  • rotate the int to the right by 4 bits and store back in the int var. (be sure to save the "right-most" 4 bits in a temporary variable before shifting, and DON'T sign-extend)
  • print the var. in hexadecimal (use %X with System.out.printf)

Problem C
Write another Java application program to find and display all of the Perfect Numbers between 2 and 500 (inclusive). A Perfect Number is a number for which the sum of its divisors (between 1 and number/2, inclusive) is equal to the number. For example, 6 is a Perfect number (1 + 2 + 3 = 6).
You MUST include in this program:
1. for loops
2. % (modulus) operator to determine if a number is a divisor
3. static final (class-scope) variables for 2 and 500.
4. OPTIONAL: static method to return a boolean value (true IF THE PARAMETER IS A PERFECT NUMBER) by finding and adding the divisors (between 1 and parameter/2, inclusive) of the integer parameter and comparing the sum to the parameter.

DO NOT USE CLASS-SCOPE VARIABLES unless they're final AND static.
Do not use any input for these programs!



Solution

/*
Name of program: Display Information given in Problem A
Programmer's name: ABC
Current Date: DD/MM/YYYY
Computer system and compiler you are using: Win 8, JDK 1.7
Brief description of the program (1-5 sentences): LINE UP YOUR INFORMATION, INCLUDING PUTTING NEWLINES AS SHOWN. You don't need any variables.
Variable names (if any) and descriptions of what they represent: Used /n (newline), %5s (for 5 empty space at the beginning of line)
*/

public class HW1_A 
{
/** @param args */
public static void main(String[] args) 
{
// HW1 Problem A
System.out.println("CIS 35A Student Learning Outcomes\n");
System.out.printf("Outcome 1:\n%5sRead, analyze and explain intermediate level Java\n%5sprograms.\n\n", " ", " ");
System.out.printf("Outcome 2:\n%5sDesign solutions for intermediate level problems using\n%5sappropriate design methodology incorporating object-\n%5soriented intermediate programming constructs.\n\n", " ", " ", " ");
System.out.printf("Outcome 3:\n%5sCreate algorithms, code, document, debug, and test\n%5sintermediate level Java programs.", " ", " ");
}
}

/*
Program Output:
Student Learning Outcomes

Outcome 1:
     Read, analyze and explain intermediate level Java
     programs.

Outcome 2:
     Design solutions for intermediate level problems using
     appropriate design methodology incorporating object-
     oriented intermediate programming constructs.

Outcome 3:
     Create algorithms, code, document, debug, and test
     intermediate level Java programs.
*/


/*
Name of program: Problem B - Rotate the int to the right by 4 bits in loop 3 times
Programmer's name: ABC
Current Date: DD/MM/YYYY
Computer system and compiler you are using: Win 8, JDK 1.7
Brief description of the program (1-5 sentences): Write another Java application program which initializes an int variable to 0x468A does the following in a loop 3 times:
- rotate the int to the right by 4 bits and store back in the int var. (be sure to save the "right-most" 4 bits in a temporary variable before shifting, and DON'T sign-extend) 
- print the var. in hexadecimal (use %X with System.out.printf)
Variable names (if any) and descriptions of what they represent: i_origNum - to Store original number, i_tempNum - to save the "right-most" 4 bits in a temporary variable, i_resultNum - to store result value
*/
public class HW1_B 
{
/** @param args */
public static void main(String[] args) 
{
int i_origNum = 0x468A;
int i_resultNum = i_origNum; // Set default value
// loop 3 times
for (int i = 0; i < 3 ; i++)
{
int i_tempNum = 0;
i_tempNum = (i_resultNum & 0x00F) << 12; // use masking to get last 4 binary digit and left shift by 12 bits put them to right most position
i_resultNum = (i_resultNum >>> 4) + i_tempNum; // rotate right by 4 bits and than add temp number as right most digit
System.out.printf("%X %X %X\n", i_origNum, i_tempNum, i_resultNum); // print all 3 numbers in hex format
}
}
}
/*
Program Output:
468A A000 A468
468A 8000 8A46
468A 6000 68A4
*/


/*
Name of program: Problem C - Perfect Number
Programmer's name: ABC
Current Date: DD/MM/YYYY
Computer system and compiler you are using: Win 8, JDK 1.7
Brief description of the program (1-5 sentences): Write another Java application program to find and display all of the Perfect Numbers between 2 and 500 (inclusive). A Perfect Number is a number for which the sum of its divisors (between 1 and number/2, inclusive) is equal to the number. For example, 6 is a Perfect number (1 + 2 + 3 = 6). You MUST include in this program:
1. for loops
2. % (modulus) operator to determine if a number is a divisor
3. static final (class-scope) variables for 2 and 500.
4. OPTIONAL: static method to return a boolean value (true IF THE PARAMETER IS A PERFECT NUMBER) by finding and adding the divisors (between 1 and parameter/2, inclusive) of the integer parameter and comparing the sum to the parameter.
Variable names (if any) and descriptions of what they represent: 
*/
public class HW1_C 
{
public static final int i_rangeMin = 2; // Search between 2 to 500 (inclusive)
public static final int i_rangeMax = 500;

public static void main(String[] args) 
{
for (int i=i_rangeMin; i <= i_rangeMax; i++) // Outer loop go through all number between 2-500
{
int i_sumOfDivisors = 0;
for (int j=1; j <= (i/2); j++) // Inner loop search for divisors and 
{
if(i%j == 0)
i_sumOfDivisors = i_sumOfDivisors + j; // Sum of divisors
}
if (i_sumOfDivisors == i) // Comparison to check whether it is a Perfect number
System.out.println(i);
}
}
}

/*
Program Output:
28 
496 
 */


Comments