Wednesday, December 19, 2018

Lab 10 (Week 13 13/12/2018)

Heyyy, we had lab 10 which is our last lab.
It is about two dimensional array.
What is two dimensional array??
Let me explain it !
The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns. 
For example, this creates a 2D array of int that has 12 elements arranged in 3 rows and 4 columns.
int[][] A;
A = new int[3][4];
int[][]  A  =  {  {  1,  0, 12, -1 },
                  {  7, -3,  2,  5 },
                  { -5, -2,  2, -9 }
               };
Let looks at some question for Lab10.
Question 1: Write a program that uses a String array to hold the five students’ names, an array of five characters to hold five students’ letter grades, and five arrays of four doubles each to hold each student’s set of test scores. The program should have methods that return a specific students’ name, average test score, and a letter grade based on the average. 
**Several method is invoked or called which are getName(). getScore(), getTotal(), getAverage() and getGrade()

The name of students is stored, followed by their scores for four test and total. Next, the average is counted and last but not least grade is determined based on the average.


Question 2 : Write the following method that returns the location of the largest element in a two dimensional array.

public static int[] locateLargest(double[][] a) 

The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two dimensional array. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. 


**First row and column is entered and their elements are entered by user.
The largest element is found using loops.
It quite complicated, but with the helps of friends I am able to solve it. Thanks them very much. 


Question 3 is asked to (a)find the smallest number and  (b) print a reverse of the array by considering the program given.
(a) The smallest number is found by using usual way.
(b) I am still finding the way to solve this.






This question is asked us to write a program that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program will generate number randomly between 1 and 100; then produce a chart similar to the one below indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.
The answer is as following : 

* This question is solved by using array and loops.
The histogram is constructed by printing "*" in cmd. The numbers is entered until the number entered is not in range 1-100. If number is out of 1-100, the program will end and the histogram is displayed.
The "*" print out in which rows is depend on the numbers entered.

Let's do a summary for the lecture from week 1 to14. It's a wonderful class we attended. We have a very good and kind lecturer named En. Erzam. He always like to make joke in class and taught us step by step.
Through this course, we are able to know what is programming. Every program does not come easily and successful without a good programming.
In short, what we learned throughout these 14 weeks is selection, loops, method, string and array. We learned one by one. Firstly, we know how to solve selection problem. Next, we know how to use loop to do repetition. Thirdly, we know how to call or invoke method to make programming easily. Moreover,  we know how to use string in constructing the code. Last but not least, array is used to   store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
I am very happy and satisfied because I get such amazing knowledge and hope to explore in future.

 That's all for our lab. Good luck and all the best to our final.^^
Thank you very much ! Byeeee ^^
 
LIM XUE TING
183615
SSK3100 (Group15)
Next blog: hennyabigail.blogspot.com (Henny Abigailwillyen Sinjus)

Lab 9 (Week 12 6/12/2018)

Hey! Today is week 12. We had new lab 9 which is about array.

Array is a data structure that represents a collection of the same types of data.

Let's look at some example to understand array in more detail.
The question 1 is asked to write statement for array.

a.       Create an array to hold 10 double values.



double [ ] values = new double [10];



b.      Assign the value 5.5 to the last element in the array.

values [values.length – 1] = 5.5;

c.       Display the sum of the first two elements.

System.out.println (values [ 0 ] + values[ 1 ]);

d.      Write a loop that computes the sum of all elements in the array.

double sum = 0;
for (int i = 0; i < values.length; i++)
                              sum += values [ i ];

e.       Write a loop that finds the minimum element in the array.

double min = values [ 0 ];
for (int i = 0; i < values.length; i++)
if (min > values [ i ] )      
min= values [ i ];

f.       Randomly generate an index and display the element of this index in the array.

System.out.println (values [ (int) (Math.random ( )*numbers.length) ] );

g.      Use an array initializer to create another array with the initial values 3.5, 5.5, 4.52 and 5.6.

double [ ] values = {3.5, 5.5, 4.52, 5.6};

*This is how array created !!!!  
 


The question 2 is to identify and fix the errors in the following code that attempts to assign random numbers to an array of 100 integers: public class Test { 
public static void main(String[] args)  {
double[100] r;
for ( int i = 0; i < r.length();  i++ )   { 
r(i) = Math.random * 100;
}  } *The way declare array is WRONG .


The question 3 is asked to write a program that reads in n integers and sort them in increasing order.
* The numbers are displayed in increasing order.


The question 4 is asked to write a program that randomly generates n integers between 0 and 10 and counts the occurrence of each.


Question 5 : Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average, and how many scores are below the average. Enter a negative number to signify the end of the input. Assume maximum score is 100.
*Negative number is used to end the program by using the loops.


Question 6 : Write two overloaded methods that return the average of an array with the following headers: 

public static int average(int[] array)
public static double average(double[] array) 

Write a test program that prompts the user to enter 10 double values, invokes this method, then displays the average value.  
*Two method average with different datatype which are int and average lead to different answers. 


Question 7 : Write a method that finds the largest element in an array of double values using the following header: 

public static double max(double[] array) 


Write a test program that prompts the user to enter ten numbers, invokes this method to return the maximum value, and displays the maximum value.  

* method max is invoked.


Question 8 : Write a program that prompts the user to enter the number of students, the students’ names, and their scores and prints student names in decreasing order of their scores. Assume the name is a string without spaces, use the Scanner’s next() method to read a name. 
*The score for students is displayed in decreasing order.




 

Question 9 : Re-write the FindingRoot program from your previous lab to find roots for the equation 
y = (x-4)2 – 9
This program should now be able to process n binary strings instead of just 4. The modified FindingRoot program should:
i. Put the n binary strings in an array called population.
ii. Sort the elements in population by their fitness values. The binary strings with the larger fitness values will be put in the beginning of population.
iii. Perform the crossover operations on the elements in population.
iv. Replace every element in population with the new binary strings.
v. Find the average fitness value of the elements.
vi. Changing the termination criteria in step (b). The loop terminates when the average fitness value for 20 iterations differs by only 0.1 or less.

It quite complicated, we will try to investigate and get the solution for this question.

In short, array is important in programming because :
  • It is better and convenient way of storing the data of same datatype with same size.
  • It allows us to store known number of elements in it.


That's all for today. Thank you very much ^^


LIM XUE TING
183615
SSK3100 (Group15)
Next blog: hennyabigail.blogspot.com (Henny Abigailwillyen Sinjus)

Lab 10 (Week 13 13/12/2018)

Heyyy, we had lab 10 which is our last lab. It is about two dimensional array. What is two dimensional array?? Let me explain it ! The e...