Please complete the following programming with clear explanations. Thanks! Homew
ID: 3877342 • Letter: P
Question
Please complete the following programming with clear explanations. Thanks!
Homework 1 – Programming with Java:
What This Assignment Is About:
Classes (methods and attributes)
Objects
Arrays of Primitive Values
Arrays of Objects
Recursion
for and if Statements
Selection Sort
Use the following Guidelines:
Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
Use white space to make your program more readable.
For each file (class) in your assignment, provide a heading (in comments) which includes:
The assignment number.
Its author (your name).
A description of what this program is doing.
3. Part 1. Primitive Types, Searching, Recursion (35 points).
a) Create a class Homework (in a file Homework.java)
b) Create a static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 1s in the odd positions of the array and 0s in the even positions.
c) Create a static method printArray that receives as a parameter an array of integers. Use
a while statements to print all the elements in the array.
d) Create a static method arraySelectionSort that receives as a parameter an array of integers and order its element in ascending order. Implement Selection Sort algorithm. It should be Selection Sort, not Bubble Sort, not Quick Sort, etc. If you do not remember selection sort, this link could be useful: https://goo.gl/hrAdMo
e) Create a static recursive method that calculate and returns the factorial a number. The method receives the number (integer number) as parameter
f) Copy the following main method in your class,
public static void main (String [] arg) {
int [] a = {3, 5, 6, 8, 12, 13, 16, 17, 18, 20}; int [] b = {18, 16, 19, 3 ,14, 6};
int [] c = {5, 2, 4, 3, 1};
// testing initializeArray
printArray(a); // print: 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 initializeArray(a);
printArray(a); // print: 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
// testing initializeArray
printArray(b); // print: 18, 16, 19, 3 ,14, 6 arraySelectionSort (b);
printArray(b); // print: 19, 18, 16, 14, 6, 3
// testing factorial
System.out.println (factorial (5)); //print: 120
c[0] = factorial (c[0]);
c[1] = factorial (c[2]);
printArray(c); // print: 120, 24, 4, 3, 1
}
4. Part 2 Classes, Objects, and Arrays:
In this assignment, we will be making a program that reads in student’s information, and create a classroom seating arrangement with a number of rows and columns specified by a user. Then it will attempt to assign each student to a seat in an auditorium.
Use the file Assignment.java (attached at the end of this document). Do not change the content of this file.
Save all files in the same folder.
Step 1.
First, you need to create Student.java file by defining Student class. It should have two instance variables, lastName (String) and firstName (String). In addition, the following methods should be defined.
Step 2.
You will be creating a class called ClassroomSeating. This class should be defined in a file named " ClassroomSeating.java". The class ClassroomSeating will contain a 2-dimensional array called "seating" of Student objects at its instance variable. The class ClassroomSeating must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.)
Please see the sample output listed below.
After compiling Student.java, ClassroomSeating.java, and Assignment.java files, you need to execute Assignment.class.
Sample Output: (the inputs entered by a user are shown in bold)
Make sure that your program works at least with this scenario.
C:MyJavapplications>java Assignment.
CODE:
public class Assignment {
public static void main(String[] args) {
ClassroomSeating classSeating; Student tempStudent;
int row, col, rowNum, columnNum; String studentInfo;
Scanner scan = new Scanner(System.in);
// Ask a user to enter a number of rows for an auditorium seating System.out.println
("Please enter a number of rows for an auditorium seating."); rowNum = scan.nextInt();
// Ask a user to enter a number of columns for an auditorium seating System.out.println
("Please enter a number of columns for an auditorium seating."); columnNum = scan.nextInt();
// instantiate a ClassroomSeating object
classSeating = new ClassroomSeating(rowNum, columnNum);
System.out.println
("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/ studentInfo = scan.next();
/* we will read line by line **/
while (!studentInfo.equalsIgnoreCase("Q")){
System.out.println(" A student information is read."); // printing information read from a file. System.out.println(studentInfo);
// creating a student object using the student information from a user tempStudent = new Student(studentInfo);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
System.out.println
("Please enter a row number where the student wants to sit."); row = scan.nextInt();
System.out.println
("Please enter a column number where the student wants to sit."); col = scan.nextInt();
// Checking if the row number and column number are valid // (exist in the theatre that we created.)
if (classSeating.checkForBoundaries (row, col) == false) { System.out.println(" row or column number is not valid."); System.out.println ("A student " + tempStudent.getFirstName() +
" " + tempStudent.getLastName() + " is not assigned a seat."); } else {
// Assigning a seat for a student
if (classSeating.assignStudentAt(row,col,tempStudent) == true){
System.out.println(" The seat at row " + row + " and column " + col + " is assigned to the student " + tempStudent.toString()); System.out.println(classSeating);
} else {
System.out.println(" The seat at row col + " is taken.");
} }
// Read the next studentInfo System.out.println
("Please enter a student information or /*** reading a student's information ***/ studentInfo = scan.next();
} }
}
" + row + " and column " +
enter "Q" to quit.");
Please see the sample output listed below.
Method Description of the Method instance variables, lastName and firstName. Constructs a Student object using the string containing student's info. Use public Student ()Constructs a Student object by assigning the default string " " to both public Student (String studentlnfo) the split method of the String class to extract first name and last name, then assign them to each instance variable of the Student class. Arn example of the input string is: David-Johnson public String It should return the instance variable lastName, etLastName public String getFirstName () It should return the instance variable firstName. public String toString () It should constructor a string containing the initial character of the first name, a dash, the initial character of the last name, and a period, then it returns it. An example of such string for the student David Johnson is: D-JExplanation / Answer
4.Part 2
/**
* The java program Assignment that prompts user to enter
* number of rows and columns of theater seating. Then prompts
* for name of the seater and assign the name to the corresponding
* seat number if valid otherwise print a message.
* */
//Assignment.java
import java.util.Scanner;
public class Assignment
{
public static void main(String[] args)
{
ClassroomSeating classSeating; Student tempStudent;
int row, col, rowNum, columnNum; String studentInfo;
Scanner scan = new Scanner(System.in);
// Ask a user to enter a number of rows for an auditorium seating
System.out.println("Please enter a number of rows for an auditorium seating.");
rowNum = scan.nextInt();
// Ask a user to enter a number of columns for an auditorium seating
System.out.println("Please enter a number of columns for an auditorium seating.");
columnNum = scan.nextInt();
scan.nextLine();
// instantiate a ClassroomSeating object
classSeating = new ClassroomSeating(rowNum, columnNum);
System.out.println
("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/
studentInfo = scan.nextLine();
/* we will read line by line **/
while (!studentInfo.equalsIgnoreCase("Q"))
{
System.out.println(" A student information is read.");
// printing information read from a file.
System.out.println(studentInfo);
// creating a student object using the student information from a user
tempStudent = new Student(studentInfo);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
System.out.println("Please enter a row number where the student wants to sit.");
row = scan.nextInt();
System.out.println("Please enter a column number where the student wants to sit.");
col = scan.nextInt();
scan.nextLine();
// Checking if the row number and column number are valid
// (exist in the theater that we created.)
if (classSeating.checkForBoundaries (row, col) == false)
{
System.out.println(" row or column number is not valid.");
System.out.println ("A student "
+ tempStudent.getFirstName()
+" " + tempStudent.getLastName()
+ " is not assigned a seat.");
}
else
{
// Assigning a seat for a student
if (classSeating.assignStudentAt(row,col,tempStudent) == true)
{
System.out.println(" The seat at row " + row + " and column " + col + " is assigned to the student " + tempStudent.toString());
System.out.println(classSeating);
}
else
{
System.out.println(" The seat at "
+row+" and "+ col +"is taken.");
}
// Read the next studentInfo
System.out.println("Please enter a student information or enter "Q" to quit.");
/** reading a student's information **/
studentInfo = scan.nextLine();
}
}
}
}
---------------------------------------------------------------------------------------------------
//ClassroomSeating.java
public class ClassroomSeating
{
private int numRows;
private int numCols;
//declare a 2d-array
private Student seats[][];
//constructor to initialize the array of student type
public ClassroomSeating(int numRows,
int numCols)
{
this.numRows=numRows;
this.numCols=numCols;
//create an array of Student object
seats=new Student[numRows][numCols];
//instantiate each student object
for (int i = 0; i < numRows; i++)
{
for (int j = 0;
j < numCols; j++)
{
//instantiate Student object
seats[i][j]=new Student();
}
}
}//end of constructor
/*Returns student at row and col positions*/
private Student getStudentAt(int row,int col)
{
return seats[row][col];
}
/**Method that takes row ,col and Student object and
* set the Student object to seats at row and col
* and return true otherwise returns false*/
public boolean assignStudentAt
(int row,int col,Student tempStudent)
{
if(seats[row][col].getFirstName().equals("***") &&
seats[row][col].getLastName().equals("***"))
{
seats[row][col]=tempStudent;
return true;
}
else
return false;
}
/**Check for the boundaries of seating array*/
public boolean checkForBoundaries(int row,int col)
{
if((row<0 || col<0) || (row>numRows || col>numCols))
return false;
else
return true;
}
/*Override the toString method*/
public String toString() {
String desc="";
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
desc+=getStudentAt(i, j)+" ";
}
desc+=" ";
}
return desc;
}
}
---------------------------------------------------------------------------------------------------
//Student.java
public class Student
{
//private data members of class
private String firstName;
private String lastName;
//constructor of class
public Student()
{
firstName="***";
lastName="***";
}
//constructor that takes string and set name to the
//classs first name and last name
public Student(String studentInfo)
{
//split name by space
String data[]=studentInfo.split("-");
//set first name
firstName=data[0];
//set last name
lastName=data[1];
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
/*Override tostring method*/
public String toString()
{
return firstName.charAt(0)
+"-"+lastName.charAt(0);
}
}//end of the class Student
---------------------------------------------------------------------------------------------------
Sample Output:
Please enter a number of rows for an auditorium seating.
3
Please enter a number of columns for an auditorium seating.
3
Please enter a student information or enter "Q" to quit.
Mickey-Mouse
A student information is read.
Mickey-Mouse
Please enter a row number where the student wants to sit.
1
Please enter a column number where the student wants to sit.
2
The seat at row 1 and column 2 is assigned to the student M-M
*-* *-* *-*
*-* *-* M-M
*-* *-* *-*
Please enter a student information or enter "Q" to quit.
Daisy-Duck
A student information is read.
Daisy-Duck
Please enter a row number where the student wants to sit.
2
Please enter a column number where the student wants to sit.
0
The seat at row 2 and column 0 is assigned to the student D-D
*-* *-* *-*
*-* *-* M-M
D-D *-* *-*
Please enter a student information or enter "Q" to quit.
Clarabelle-Cow
A student information is read.
Clarabelle-Cow
Please enter a row number where the student wants to sit.
2
Please enter a column number where the student wants to sit.
1
The seat at row 2 and column 1 is assigned to the student C-C
*-* *-* *-*
*-* *-* M-M
D-D C-C *-*
Please enter a student information or enter "Q" to quit.
Max-Goof
A student information is read.
Max-Goof
Please enter a row number where the student wants to sit.
0
Please enter a column number where the student wants to sit.
0
The seat at row 0 and column 0 is assigned to the student M-G
M-G *-* *-*
*-* *-* M-M
D-D C-C *-*
Please enter a student information or enter "Q" to quit.
Horace-Horsecollar
A student information is read.
Horace-Horsecollar
Please enter a row number where the student wants to sit.
5
Please enter a column number where the student wants to sit.
1
row or column number is not valid.
A student Horace Horsecollar is not assigned a seat.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.