Starting from:
$35

$29

Assignment 1 (7 Questions) Solution




QUESTION 1 (10 Marks) Formatting Console Output







Show the output of the following statements.































Write your answers as the two examples given below:








































Note: There is a missing \f " after \5.2" in (b).




QUESTION 2 (15 Marks) Methods







A pentagonal number is de ned as n(3n the rst few numbers are 1, 5, 12, 22, . returns a pentagonal number:




1)=2 for n = 1; 2; ; and so on. Therefore, Write a method with the following header that


















1






Write a test program with the class name TestPentagonalNumber which uses the above method to display the rst 100 pentagonal numbers with 10 numbers on each line. The method getPentagonalNumber is to be invoked in your main method. Each pentagonal number occupies 6 spaces and is left justi ed. Use proper CamelCase Notation for all identi ers in your program.




QUESTION 3 (10 Marks) Basics of One-Dimensional Arrays







Once an array is created, its size cannot be changed. Does the following code resize the array? Explain your answer.


















Suppose the following code is written to reverse the contents in an array, explain why it is wrong. How do you x it?

































(3) Show the output of the following code:




























Compile and run the following program in command-line environment and make screenshots to show your results.
























Use the following commands one by one:










2






ls command: The listed les and directories should include \Test.java" and no \Test.class".



javac command: Use it to compile your \Test.java", which produces \Test.class".



ls command: The listed les and directories should now include both \Test.java" and \Test.class".



java command: Use it three times to invoke the program as follows:



java Test I have a dream java Test \1 2 3"




java Test




Your screenshots should include all the above commands, and the outputs of the three invocations. (Hint: You may need to import java.lang.String in the program.)







QUESTION 4 (15 Marks) Programming with One-Dimensional Arrays







Write two overloaded methods in a class named AverageOfArray that return the average of an array with the following headers:













Write a main method that prompts the user to enter ten double values, invokes this method, and displays the average value. Use CamelCase Notation for all identi ers in your program.




QUESTION 5 (15 Marks) Basics of Multidimensional Arrays







Show the output of the following codes:




(1)






















(2)






















3



















































(3)



















QUESTION 6 (20 Marks) Programming with Multidimen-sional Arrays







An n n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following method in a class named MarkovMatrix to check whether a matrix is a (positive) Markov matrix.













Write a main method that prompts the user to enter a 3 3 matrix of double values and tests whether it is a Markov matrix. Here are sample runs:
































































4






QUESTION 7 (15 Marks) Strings and Arrays







Write a main method in a class named MaximumIntegerWithCommandLine to get integers from command line arguments and display the maximum of these integers. The user can arbitrarily input any number of integers. If no integer is input from user, your program displays the following message and terminates.




Usage: java MaximumIntegerWithCommandLine integer1 integer2 ...




The following screenshot is a sample run.




























Hint: You need to invoke proper method to convert String to int.







































































































5

More products