C1022 Introduction to Java Term 1 2008 assignment
Hi! ![]()
This time I will post the assignment that I get for C1022 Introduction to Java module in Term 1 2008.
SECTION A
A1. Write Java statements to accomplish each of the following:
a) Declare a variable ‘reply’ to be of type char.
char reply;
b) To compare the contents of both variables ‘pass’ and ‘word’ of type String.
pass.compareTo (word) > 0;
pass.compareTo (word) == 0;
pass.compareTo (word) < 0;
pass.compareTo (word) > 0;
pass.equals (word);
pass.equalsIgnoreCase (word);
c) To assign a float value 2.5 to a variable ‘number.
float number = 2.5f;
d) Display a message ‘happy new year’ using a method from the Systems class.
System.out.println(“happy new year”);
e) Declare a method ‘view’ that does not return a value and has no arguments.
public static void view()
{
System.out.println(“A method without arguments.”);
}
A2. Draw the following symbols used in a program flowchart:
a) Process

b) Connector

c) Input
![]()
d) Decision

e) Start

A3. State which of the following statements are CORRECT or INCORRECT.
a) It is not an error not to end a Java file name with the .java extension. Incorrect
b) Omitting the semicolon at the end of a statement is a syntax error. correct
c) The + operator can be used for string concatenation. Correct
d) Infinite loops are caused when the loop-continuation condition in an iteration structure never becomes false. Correct
e) The method toUpperCase() converts any string or character to its uppercase equivalent. Correct
f) A TextField must be created with initial text and with or without a specified size. Incorrect
g) A loop that never ends is called an infinite loop. Correct
h) To import an entire package of classes, an asterisk(*) can be used as a wildcard symbol to represent all the classes in a package. Correct
A4. Identify the layout manager, component or method which will perform the following:
a) A window component into which a user can type a single line of text data.
Textfield
b) A built-in class that holds text that can be displayed within an applet.
Label
c) A component which is used to display a series of items from which the user may select one or more items.
List
d) The user requires the name entered to be stored in lower case.
toLowerCase()
e) A window component which allows the user to click to trigger a specific action.
Button
f) A layout manager which allows the programmer to arrange the window components into five areas: north, south, east, west and center.
BorderLayout
A5. State the value of each of the following Boolean expressions.
a) 2 + 17 * 3 = = 21 False
b) 17 / 2 + 2.5 * 3 >= 18 False
c) 25 % 10 * 5 < 40 True
d) 27 % 9 != 4 True
e) 18 / 2 + 6 / 2 != 12 False
f) (3 +
% 5 >= 5 False
A6. Identifiers are names that are given to variables, classes or methods. List the THREE requirements which must be met when declaring identifiers.
1. Identifier must begin with letter, an underscore ( _ ), or a dollar sign ( $ ).
2. Identifier cannot have any space or other symbols.
3. There is no maximum length of characters that must be used in identifier.
A7. Name any TWO types of access modifier that define the circumstances under which a class or class members in programs can be accessed.
Types of access modifier :
1. Public
2. Private
SECTION B
[60 marks]
Answer ALL questions in this section.
B1. a) Identify and correct the errors in each of the following codes:
i) The following should print whether integer ‘value’ is odd or even:
switch(value % 2)
{
case 0 : System.out.println(“Even integer”);
case 1 : System.out.println(“Odd integer”);
}
a. Error Identification:
There are some errors in the above code. First, the variable “value” hasn’t been declared. Second, there is no “break” statement at the end of each case; we will need the break statement at the end of each case to terminate the switch structure.
b. Correction:
The correct form of the above code will be as the following code:
int value = 3;
switch(value % 2)
{
case 0 : System.out.println(“Even integer”);
break;
case 1 : System.out.println(“Odd integer”);
break;
}
ii) public static void float(int a);
{
int a;
System.out.println(a);
}
a. Error Identification:
The errors of the above code are:
1. It use a Java reserved word (“float”) as the method name.
2. It places the semicolon (;) at the end of the method header.
3. It declare a variable “a” but “a” has been used as the method argument, so it shouldn’t use “a” as the name of variable inside the method.
4. The variable in the method didn’t have any value, so it cannot be printed.
b. Correction:
The correct form of the above code will be as the following code:
public static void MyMethod(int a);
{
int b = a + 1;
System.out.println(b);
}
b)

Write a Java segment to represent the above flowchart.
import java.io.*;
public class WhileLoop
{
public static void main(String[] args)throws Exception
{
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
// Declares the variable
int total = 0;
int number;
// Prompt the user to enter a number
System.out.print(“Enter a number: “);
number = Integer.parseInt(input.readLine());
// While-loop that will continue as long as the entered number is more than 0
while (number > 0)
{
// accumulate total variable with the number entered from the user
total = total + number;
System.out.println(“Enter 0 or negative number to display the total”);
System.out.println(“Or enter positive number to continue accumulating numbers”);
System.out.print(“Enter a number: “);
number = Integer.parseInt(input.readLine());
}
// Display the value of variable “total” when user enter 0 or negative number
System.out.println(“The total is ” + total);
}
}
c) List and explain briefly any THREE benefits of object-oriented programming.
Benefits of object-oriented programming
1. Reusability
We can reuse objects, classes, and methods that we have made before. This benefit make our job easier because we don’t have to repeat making a same object, we just need to make an object once and use it anytime we need it.
2. Adaptability
Adaptability means that Java programs can run in any environment. It is platform independent. So, as long as there is a Java compiler and Java Virtual Machine installed within a system, it can run any Java program.
3. Maintainability
Because we can easily reuse any object that we have made before, it is also easy for us to maintain the Java program because we can edit any object independently without interfering the other object.
B2.
a) Explain the difference between a counter-controlled repetition and a sentinel controlled repetition.
Difference between counter-controlled repetition and sentinel controlled repetition:
1. Counter Controlled Repetition
Counter-controlled repetition is a definite repetition. It means that we know exactly how many times the repetition will occur in a counter-controlled repetition. Furthermore, the number of repetition happen in counter-controlled repetition is controlled by a control variable. The control variable will have an initial value that will be incremented or decremented every time a repetition occurs. So, we will be able to know precisely how many repetition/loops will happen in this type of repetition.
2. Sentinel Controlled Repetition
Sentinel-controlled repetition is an indefinite repetition. It means that we don’t know how many loops will occur in a sentinel-controlled repetition. There is a sentinel value which indicates the end of the loop. When the sentinel value is entered, the loop will be end. So, we won’t know precisely how many loops will occur in this type of repetition.
b)
i) Write a method header ‘hypotenuse’ that takes two values ’side1′ and ’side2′ of type double and returns a double result.
public static double hypotenuse(double side1, double side2)
ii) Declare an array called ‘realnumbers’, which is a one dimensional array to store
numbers of type float. Assuming that the size of the array is 10
float realnumbers[] = new float[10];
c) Explain the meaning of the following Java statements.
i) Button calbutton = new Button(“calculate”);
The Java statement means that we make a button. We name the button as “calbutton” and then we give the button a label that is “calculate”
ii) g.drawLine(30, 50, 100, 50);
It means that we draw a horizontal line which start from this coordinate
(30, 50) until this coordinate (100, 50);
iii) int[] marks = {56, 70, 80};
It means we declare a one dimensional array “marks” then we initialize three values in this array, that is 56, 70, and 80.
d) Write a Java program using the drawRoundRect() method from the paint() method to draw a circle with the arguments as follows:
x1 and y1 : both 30
width and height : both 100
Assuming Graphics object g as the argument to the paint() method.
import java.awt.*;
import java.applet.*;
public class DrawRoundRect extends Applet
{
public void paint(Graphics g)
{
g.drawRoundRect(30,30,100,100,100,100);
}
}
Do you have any comment on this? maybe you have something to ask me or want to discuss with me about this?Or maybe you are IDIC student and taking this module also?
Feel free to use the comment box below (click here if you cannot see the comment box).



Come on dude, these facts* and proof* i mean who is posting* lol
lol
can u help me solve the c1022 assignment term 1 (c1022)
i am frm nepal, ktm, informatics college IDIC student
u dowmnload the assignment question frm net
and send me out the possible answer as soon as possible
hi abhay,
I dont have access to Informatics Nepal.
and please just ask the specific question that you cant do.
thank you..
best regards,
Thomas
I quite valuable posts, which superiority be of very of use representing beginners in blogging as I am. I already maintain a unimaginative collecting of blog posts and other articles, from which I in harmony close to according with learn many aspects of life. Thanks you in return your resource.
Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.
I bookmarked your post will read this latter . Regards, Mike
Nice work and post research, I have liked your blog.
Valuable information and distinguished enterprise you got here! I would like to thank you in place of sharing your thoughts and time into the stuff you despatch!! Thumbs up