Friday, December 21, 2018

...Lab10...

Assalamu'alaikum and hello


I'm going to share about the assignment that has given (Lab10) on topic array. We are required to define, declare and show the process of array. Here I include all of the tasks that I have done. 

Question 1











































Question 2











































Question 3


















































That’s all from me. Remain positive though it is impossible. Have a nice day ^_^



Nurazreen binti Mohd Nor (182433)

SSK3100 (Group 15)




Next blog : kychoong.blogspot.com (Choong Kar Yee)

...Lab9...

Assalamu'alaikum and hello..


I'm going to share to all of you my Lab 9 on the topic array. From this assignment, I need to declare, initialize and understand the process of single-dimensional array. 


Question 1

     a)      double[ ] value = new double[10];

     b)      value[value.length – 1] = 5.5;

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

     d)     double sum = 0;

for (int i = 0; i < value.length; i++)
{
sum += value[i];
}

     e)      double min = 0;

for (int i = 0; i < value.length; i++)
{
if ( min > value[i] )
min = value[i];
}

     f)       System.out.println(value[(int)(Math.random() * value.length)] );

     g)      double[ ] value = {3.5, 5.5, 4.2, 5.6};


Question 2
Line 5: Declaration is wrong, double[100]  should be double[ ] . The array needs to be create before use it, e.g new double[100] .
Line 7: r.length() should be r.length
Line 9: r(i) should be r[i]

Line 9: Math.random should be Math.random()

Question 3 
Write a program that reads in n integers and sort them in increasing order.




Question 4
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.






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.





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.





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.





That’s all from me. Remain positive though it is impossible. Have a nice day ^_^



Nurazreen binti Mohd Nor (182433)

SSK3100 (Group 15)




Next blog : kychoong.blogspot.com (Choong Kar Yee)

...Lab8...

Assalamu'alaikum and hello


I'm going to share about the assignment that has given (Lab8). We are required to define and invoke various types of methods. Here I include all of the tasks that I have done. 


Write a program that assign grades to students in a course. The program prompts the user to input all first and second tests, laboratory works and final exam marks and print out the grade for each of the student. Write the method assignGrade() that receives the mark of a student and display the grade based on the mark.





















































Write a program that has the following methods to:

     a. count the number of letters in a string using the following header:
                  public static int countLetters(String s)

















     b. find the number of occurences of a specified character in a string using the following header:

                  public static int countCharacter(String str, char a


























That’s all from me. Remain positive though it is impossible. Have a nice day ^_^



Nurazreen binti Mohd Nor (182433)

SSK3100 (Group 15)




Next blog : kychoong.blogspot.com (Choong Kar Yee)

Saturday, November 24, 2018

...Lab7...

Assalamu'alaikum and hello..

I'm going to share to all of you my Lab 7 on the topic repetitions. If you want to know, repetitions consists of while Loops, do-while Loops, for Loops, break and continue. 



Question 1


What is the output of the following code? Explain the reason.
1. int x = 8000000;
2. while ( x > 0 )
3. x++;
4. System.out.println( “x is “ + x );




x is -2147483648. When a variable is assigned a large value in size to be stored, it causes an overflow.  



Question 2




























Question 3


Convert the following while loop into a do-while loop:

Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println(“Enter an integer. The input ends if it is 0. “);
int number = input.nextInt();
while(number != 0 )
 {
sum += number;
System.out.println(“Enter an integer. The input ends if it is 0.”);
number = input.nextInt();
}


Answer :

Scanner input = new Scanner(System.in);
int sum = 0;
int number;
                       
do
{
            System.out.println("Enter an integer. The input ends if it is 0.");
            number = input.nextInt();
            sum += number;
}
while (number != 0);



Question 4


Write a for loop that prints the numbers from 1 to 100.





Question 5

Convert the following for loop statement to a while and do-while loop:

long sum = 0;
for ( int i = 0 ; i <= 1000; i++ )
{
sum = sum + I;
}


Answer:

a) while loop

long sum = 0;
int i = 0;

while (i <= 1000)                 
{
sum += i;
            System.out.println(sum);
            i++;
}

b) do-while loop

long sum = 0;
int i = 0;

do {
            sum += i;
            System.out.println(sum);
            i++;
}
while (i <= 1000); 



Question 6

Modify the following code to show how many times the println statement executed in the following code. How many times was it executed?

for ( int i = 0; i < 10 ; i++ )
{
for ( int j = 0; j < i; j++)
{
System.out.println( i * j );
}
}


Answer:

The statement was executed 45 times. 


           















Question 7


Write a program that prompts the user to enter a string and then displays the number of vowels in the string.























Question 8


Write a program that prompts the user to enter student’s name and the student’s test 1 score and finally display the name of the student with the highest score. Use the next() method in the Scanner class to read a name, rather than using the nextLine() method.





























Question 9


Write a program that plays the popular rock-paper-scissor game. A scissor can cut a paper, a rock can knock a scissor and a paper can wrap a rock. The program randomly generates a number 0, 1 or 2 representing rock, paper and scissor. Use Math.random() method to get a random double value d such that 0.0 <= d < 1.0. Thus, to generate a random integer in the range of [min,max], we can use the expression: min + (int)(Math.random() * (max-min) + 1) The program prompts the user to enter a number 0, 1 or 2 and displays a message indicating whether the user or the computer wins, loses or draws. Let the user continuously play until either the user or the computer wins three times more than their opponent.


















Question 10


Find the minimum point on the function f(x) = ( x – 2 )2 + 1 where x = [-1, 0, … , 4] using random optimization. In this optimization technique, your program should just run through all the different values of the input space ( i.e. the values x can have) and see which one would give the minimum f(x). 























That’s all from me. Remain positive though it is impossible. Have a nice day ^_^



Nurazreen binti Mohd Nor (182433)

SSK3100 (Group 15)




Next blog : kychoong.blogspot.com (Choong Kar Yee)


...Lab10...

Assalamu'alaikum and hello I'm going to share about the assignment that has given (Lab10) on topic array. We are required to def...