السَّـلام عليكُم ورحمة الله ..
لـدي مسألتين في الـ Arrays & ArrayList مـع حلـول لها .. سأضعها تِباعا ً ..
سـؤالي .. هـل الأكـواد صحيحـة .. ؟! وهـل هُناكَ حَـل آخـر أبسط وأدق مِن هذِه الحلول لـِ الأسئلة .. ؟!
1.Arrays
An (nxn) matrix that is filled with numbers 1,2,3,……,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals are the same value. For example,

Write a method is Magic() which will receive as a parameter an array like the one above and return a Boolean indicating if the array is magic square or not. Write code to test your method.
الحَــل :
import java.util.Scanner;
public class MyArray
{
public static void main (String[] args)
{
int[] rowsum=new int[4];
int[] colsum=new int[4];
int[][] number=new int[4][4];
{
int num =0;
Scanner in=new Scanner(System.in);
System.out.println("Enter 16 numbers from keywoard:");
int num1=in.nextInt();
int num2=in.nextInt();
int num3=in.nextInt();
int num4=in.nextInt();
int num5=in.nextInt();
int num6=in.nextInt();
int num7=in.nextInt();
int num8=in.nextInt();
int num9=in.nextInt();
int num10=in.nextInt();
int num11=in.nextInt();
int num12=in.nextInt();
int num13=in.nextInt();
int num14=in.nextInt();
int num15=in.nextInt();
int num16=in.nextInt();
}
System.out.println(number.length);
System.out.println(number[0].length);
for(int row=0; row<number.length; row++)
for(int col=0; col<number[row].length; col++)
number[row][col]= row*4+col;
System.out.println("The original array is:");
for(int row=0; row<number.length; row++)
{
for (int col=0; col<number[row].length; col++)
System.out.print(number[row][col]+"\t");
System.out.println();
}
for(int row=0; row<number.length; row++)
{
for(int col=0; col<number[row].length; col++)
{
rowsum[row]=rowsum[row]+number[row][col];
colsum[col]=colsum[col]+number[row][col];
}
System.out.println();
}
for(int row=0; row<number.length; row++)
System.out.println(rowsum[row]+"\t");
System.out.println();
for(int row=0; row<number.length; row++)
System.out.println(colsum[row]+"\t");
System.out.println();
}
}2.ArrayList class
Your are to write a simple address book. An address book stores names, addresses and phone numbers of different people. An address book can contain many entries. Each entry is an AdBook object that belongs to the following class:
/**
*Models a single entry in address book
*/
class AdBook{
private String name;
private String address;
private String phoneNumber;
//constructor
public AdBook(String_name, String address, String_phone){…}
//get and set methods for each of the above fields
}
a.Write the complete AdBook class by completing the constructor and writing all the get() and set() methods for all the fields.
b.Write the class AddressBook that use an ArrayList and contains the following methods:
* A method addEntry(Entry e) to add a new AdBook to the address book – you must be able to add any number of entries.
* A method compareEntry(Entry e) to check if there is a same entry in the address book.
* A method printAllEntries() to print all the entries in the address book with suitable fields headers.
الحَــل :
import java.util.ArrayList;
public class Book
{
public static void main (String[] args)
{
ArrayList<String>band = new ArrayList<String>();
int n=7;
band.add("Ahmad");
band.add("Abdullah");
band.add("Ali");
band.add("Fatma");
band.add("Shooq");
System.out.println(band);
System.out.println("Size of the band:"+band.size());
for(String str:band)
{
System.out.println("The name "+str+" has "+str.length()+" characters");
}
System.out.println("The longest name band: "+" ");
if(n<6)
System.out.println("Ahmad");
if(n<4)
System.out.println("Abdullah");
if(n<5)
System.out.println("Ali");
if(n<6)
System.out.println("Fatma");
if(n==7)
System.out.println("Shooq");
else
System.out.println("No");
}
}وفق الله مَن أعانني على الصّواب الأمثـل دائما ً وأبـدا ً :) ..
