id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

simona1@sfusd.edu two_d_arrays > identity
prev  |  next  |  chance

Write a method named identity that returns a string representation of a two-dimensional array of int. The parameter is the number of rows (the number of columns is the same.) This square matrix should be an identity matrix, with all the elements 0 except the main diagonal, which should be 1's. For example, identity(3) should return {{1,0,0},{0,1,0},{0,0,1}}. Create the array in this method, then use toString() to return the array as a string. You must cut and paste method toString() from the Hint.


identity(1) → "{{1}}"
identity(2) → "{{1,0},{0,1}}"
identity(3) → "{{1,0,0},{0,1,0},{0,0,1}}"

...Save, Compile, Run (ctrl-enter)

public String identity(int a) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 315 Post-solution available

Copyright Nick Parlante 2017 - privacy