In today’s class we have learnt about fields, constructor and methods. The following practice help me go through the knowledge I absorbed. And this set of exercise reinforce my knowledge in dealing with fields, constructor and methods, especially calling of the method.
Code 1
The first code is about the all-in-one program. A much clearer structure of the previous Grade Book. This is helpful for the users to call method ,understand and rewrite the code. It can be used as an practice for all beginners of java. Source Code on Class Website.
import java.util.*;
public class Practice2 {
public Practice2(){
int[] a =new int[1000];
}
public static int Average(int numberofElements,int array[]){
int a;
int[] array3=new int[1000];
array3=array;
a=numberofElements;
int sum=0;
for(int i=1;i<=a;i++){
sum+=array3[i];
}
return sum/a;
}
public static int[] Sort(int numberofElements,int[] array){
int a;
int[] array1=new int[1000];
array1=array;
a=numberofElements;
int mark;
int temp;
for(int i=1;i<=a;i++){
mark=i;
for(int j=i+1;j<=a;j++){
if(array1[j]<array1[mark]){
mark=j;
}
}
temp=array1[mark];
array1[mark]=array1[i];
array1[i]=temp;
}
return array1;
}
public static int Search(int find, int numberofElements,int
array[]){
int target = find;
int start =1;
int end = numberofElements;
int mid = (start+end)/2;
int[] array2=new int[1000];
array2=array;
while(array2[mid]!=find&&end>start){
if(array2[mid]<find){
start = mid+1;
}
else if(array2[mid]>find){
end = mid -1;
}
mid = start + (end-start)/2;
}
return mid;
}
public static void main(String[] args){
int[] arrayForCalculation =new int[1000];
int numberOfElements;
int sum=0,min=1000,max=0,s=0,l=0,temp1=0,temp2=0;
System.out.print("Please enter the amount of numbers
you are going to put in:");
Scanner input= new Scanner(System.in);
numberOfElements= input.nextInt();
for(int i = 1; i<=numberOfElements; i++){
System.out.printf("Please enter student "+i+"'s
grade:");
arrayForCalculation[i]=input.nextInt();
}
int answer;
answer=Average(numberOfElements,arrayForCalculation);
System.out.println("The average: "+answer);
int[] output=new int [1000];
output=Sort(numberOfElements,arrayForCalculation);
for(int i=1;i<=numberOfElements;i++){
System.out.print(output[i]+" ");
}
System.out.print("Input the score you would like to
search for: ");
int search;
search = input.nextInt();
System.out.println("The student's id is:
"+Search(search,numberOfElements,output));
}
}
Input & Output
Code 2
The second code is about book storage program without a GUI. This is helpful for the users to call method ,understand and rewrite the code. It can be used as an practice for all beginners of java. Also, it is a good understanding of the concept of fields, constructor and method. Source Code on Class Website.
import java.util.*;
public class Practice3 {
private String author;
private String title;
private int pages;
private static String refNumber;
private int borrowed;
private boolean courseText;
public Practice3(){
author = "";
title = "";
pages=0;
refNumber = "";
courseText=false;
}
public static String GetAuthor(String inputAuthor){
String bookAuthor;
bookAuthor=inputAuthor;
return bookAuthor;
}
public static String GetTitle(String inputTitle){
String bookTitle;
bookTitle=inputTitle;
return bookTitle;
}
public static int GetPages(int inputPages){
int bookPages;
bookPages=inputPages;
return bookPages;
}
public static String GetRefNumber(String inputRefNumber){
String bookRefNumber;
Scanner input1=new Scanner(System.in);
while(refNumber.length()<3&&refNumber.length()!=0){
System.out.println("Please re-enter a three digit
reference number:");
refNumber = input1.nextLine();
}
bookRefNumber=inputRefNumber;
return bookRefNumber;
}
public int Borrowed(){
return ++borrowed;
}
public boolean isCourseText(String identification){
if(identification.equals("yes")){
courseText=true;
}
return courseText;
}
public static void main(String[] args){
Practice3 newBook = new Practice3();
Scanner input = new Scanner(System.in);
System.out.print("Please enter the book's title: ");
newBook.title = input.nextLine();
System.out.print("Please enter the book's author: ");
newBook.author = input.nextLine();
System.out.print("Please enter the book's Reference
Number: ");
newBook.refNumber = input.nextLine();
System.out.print("Please enter the length of the book:
");
newBook.pages = input.nextInt();
if(newBook.refNumber.equals("Please enter the number
again.")){
System.out.println("Please renter the book's
Reference Number:");
}
System.out.println("The book's title is
"+newBook.GetTitle(newBook.title));
System.out.println("The book's author is
"+newBook.GetAuthor(newBook.author));
System.out.println("The book's author is
"+newBook.GetPages(newBook.pages));
newBook.GetRefNumber(newBook.refNumber);
if(newBook.refNumber.length()>=3){
System.out.println("The book's Reference Number is
"+newBook.GetRefNumber(newBook.refNumber));
}
else if(newBook.refNumber.length()==0){
System.out.println("The book's Reference Number is
zzz.");
}
int endResult=0;
String requestInfo="";
Scanner input2 = new Scanner(System.in);
while(endResult!=1){
System.out.println("Do you want to borrow the
book?");
requestInfo=input2.nextLine();
System.out.println(requestInfo.toLowerCase());
if(requestInfo.toLowerCase().equals("yes")){
newBook.Borrowed();
}
System.out.println("Are you done with your
borrowing process?");
endResult = input.nextInt();
}
System.out.println("It has been borrowed for
"+newBook.borrowed+" times.");
String id;
System.out.println("Please identify whether it is a
textbook: ");
id=input2.nextLine();
newBook.isCourseText(id.toLowerCase());
if(newBook.courseText){
System.out.println("This is a text book.");
}
else{
System.out.println("Not a text book.");
}
}
}
Input & Output