Introduction to programming in java

Download Report

Transcript Introduction to programming in java

Introduction to programming in
java
Lecture 22
Arrays – Part 2 and Assignment No. 3
Initializer Lists
• Declare Array
– Syntax: type[] arrayName = new type[length]
– Example: int[] studentID=new int[10];
• Initializer List
– You can declare, construct, and initialize the array
all in one statement:
– Example:
int[] studentID={123,234,345,456,567,678,789,890,901}
Fill in the blanks so that the values in valA are
copied into the corresponding cells of valB.
class ArrayExp {
public static void main(String[] args) {
int[] valA = {12,23,45,56};
int valB = new int[4];
valB[0] = valA[0];
valB[1] = valA[1];
valB[2] = valA[2];
valB[3] = valA[3];
}}
Complete the assignment statement so that it
computes the sum of all the numbers in the array.
class Exercise1 {
public static void main ( String[] args ) {
int[] val = {0, 1, 2, 3};
val[0] + val[1] + val[2] + val[3] ;
sum = _________________________________
System.out.println( "Sum of all numbers = " + sum );
}
}
The length of an Array
• What is the length of the following array?
– int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
• We can use length instance variable. Examples
• System.out.println(“Length of egArray is “ + egArray.length);
• for ( int index= 0 ; index < egArray.length; index++ )
Practice Question # 1
• Set up an array to hold the following values,
and in this order:
– 3, 1, 5, 7, 4, 12, -3, 8, -2.
• Write a program which identifies the largest
number in the array.
Solution
• Practice more by yourself: find smallest number
Practice Question # 2
• Set up a string array that contains name of the
FCIT departments:
– IT, CS, IS
• Write a program which takes department
name from the user then program check
whether the user provided department
belongs to FCIT college or not.
Homework Assignment # 3
(Total marks: 5)
Submission deadline: 19th Nov 2013
NOTES:
1.
2.
3.
Only HAND WRITTEN assignments are acceptable on A4 size white
paper.
If any student copy assignment from other then -5 marks will be
deducted from the overall grade.
Late submission is not allowed.
Question 1
• Ask the user to enter in a number. Print out
that many *’ s on the screen without spaces
or newlines.
• Example:
Please enter a number:
7
*******
Question 2
• Write a program which prompts user to enter
any sentence. Program will then print the
whole sentence in reverse order.
• Example:
Enter sentence:
I like java programming.
.gnimmargorp avaj ekil I
Question 3
• Ask the user to enter in a number. Write a
program to find out if a number is prime or
not?
• Example
Enter number
13
13 is a prime number.
Question 4
• Examine the following program:
• Complete the program with four assignment statements so
that each cell of sum contains the sum of the corresponding
cells in valA and valB. I.e., add cell zero of valA to cell zero of
valB and put the result in cell zero of sum, and so on.
Question 5
• Define an integer array that contains the
following values:
int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54};
Write a program that computes
– the sum of all the elements of the array,
– the sum of the even elements, and
– the sum of the odd elements.