Title
public class Book{
private String title;
private double id;
private static int count;
protected double time;
//contructor
public Book(String title1){
time = System.nanoTime();
title = title1;
id = Math.random()*100;
count=1;
// count = (int) (1+ Math.random()*3);
}
public int getCount(){
return count++;
}
public void setCount(int count2){
count = count2;
}
public double getId(){
return id;
}
public String getTitle(){
return title;
}
//tostring method
public String toString(){
String toString;
toString = "{ID: " + this.getId() + " Title: " + this.getTitle() + " Count: " + this.getCount();
return toString;
}
public double shelfLife(){
return time;
}
public static void main(String[] args){
Book book1 = new Book("Harry Potter");
book1.getId();
//book1.getCount();
book1.toString();
System.out.println(book1.toString());
Book book2 = new Book("Umbrella Academy");
book2.getId();
// book2.getCount();
System.out.println(book2.toString());
System.out.println(book1.shelfLife());
}
}
Book.main(null);
public class Textbook extends Book{
private String company;
protected double shelfLife;
private int checkOuts;
public static ArrayList<Textbook> texts = new ArrayList<Textbook>();
public Textbook(String title2, String company2){
super(title2);
company = company2;
checkOuts = (int)(Math.random()*10);
company = "school";
texts.add(this);
}
public String getCompany(){
return company;
}
public void setCompany(String company2){
company = company2;
}
public String toString(){
String addition = " company: " + this.getCompany() + " shelfLife: " + this.shelfLife() +" }";
String fin = (super.toString() + addition);
return fin;
}
public static void main(String[] args){
Textbook text1 = new Textbook("CSA", "mort");
text1.setCount(text1.getCount());
System.out.println(text1.toString());
Textbook text2 = new Textbook("Math", "lanzi");
text2.setCount(text2.getCount()+1);
System.out.println(text2.toString());
texts.add(text1);
texts.add(text2);
for (int i=0; i< texts.size(); i++){
System.out.println(texts.get(i).toString());
}
}
}
Textbook.main(null);
public class Novel extends Book{
private String author;
private int checkOuts;
private double shelfLife;
private int pages;
public static ArrayList<Novel> novels = new ArrayList<Novel>();
public Novel(String title2, String author2){
super(title2);
author = author2;
checkOuts = (int)(Math.random()*10);
pages = 100;
novels.add(this);
}
public String toString(){
String addition = " pages: " + this.getPages() + " author: " + getAuthor() + " shelfLife: " + this.shelfLife() +" }";
String fin = (super.toString() + addition);
return fin;
}
public String getAuthor(){
return author;
}
public void setAuthor(String author2){
author = author2;
}
public void setPages(int pages2){
pages = pages2;
}
public int getPages(){
return pages;
}
public void expire(){
if(this.shelfLife>3.6){
novels.remove(this);
}
}
public static void main(String[] args){
Novel novel1 = new Novel("A", "Aadya");
novel1.setCount(novel1.getCount());
System.out.println(novel1.toString());
Novel novel2 = new Novel("B", "Avanthika");
novel2.setCount(novel2.getCount()+1);
System.out.println(novel2.toString());
Novel novel3 = new Novel("C", "Shreya");
novel3.setCount(novel3.getCount()+2);
System.out.println(novel3.toString());
for (int i=0; i< novels.size(); i++){
//novels.get(i).setCount(novel3.getCount()+1);
System.out.println(novels.get(i).toString());
}
}
}
Novel.main(null);
// Mode:
int[] array = {1,5,6,5,2,2,5,3,4,4,6,7,7,7,7,7};
// bubble sort
ArrayList<Integer> mode = new ArrayList<Integer>();
ArrayList<Integer> values = new ArrayList<Integer>();
for (int j = 0; j<array.length; j++){
for (int i = 0; i<array.length-1; i++){
if(array[i]>array[i+1]){
int temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
}
int count = 1;
for (int i = 0; i<array.length-1; i++){
if(array[i] == array[i+1]){
count++;
// System.out.println(count);
continue;
}
modes.add(count);
values.add(array[i]);
count = 1;
}
int max = 0;
int index;
for (index =0; index<mode.size(); index++){
if (mode.get(index)>max){
max = mode.get(index);
}
}
System.out.println(values.get(index));
for (int i = 0; i<array.length; i++){
System.out.print(array[i]);
}
System.out.println();
for (int i = 0; i<mode.size(); i++){
System.out.print(mode.get(i));
}
System.out.println();
for (int i = 0; i<values.size(); i++){
System.out.print(values.get(i));
}
int[] array = {1,5,6,5,2,2,5,3,4,4,6,7,7,7,7,7};
int maxNumber = -1;
int mode = -1;
for (int i = 0; i<array.length; i++){
int count=0;
for (int j=0; j<array.length;j++){
if(array[i]==array[j]){
count++;
}
}
if(count>mode){
mode = count;
maxNumber = array[i];
}
}
System.out.println(maxNumber);