Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 students 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.

image text in transcribed

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.)

image text in transcribed

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:\MyJava\applications>java Assignment.

image text in transcribed

image text in transcribed

image text in transcribed

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.");

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-J

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5advanced controlsthat you can use in a Form doesn't really specify

Answered: 1 week ago