Skip to content

Instantly share code, notes, and snippets.

@drewhoener
Created December 15, 2016 05:41
Show Gist options
  • Save drewhoener/b6ba96914eaa2208047744129308d381 to your computer and use it in GitHub Desktop.
Save drewhoener/b6ba96914eaa2208047744129308d381 to your computer and use it in GitHub Desktop.
public class Book {
int numberSold;
private String name;
private double price;
public Book(String n, String p, int c) {
}
//write the static method mostExpensive in a driver class
//which accepts an array of Books and return the price of
//the most expensive book.
public static double mostExpensive(Book[] array) {
double x = array[0].getPrice();
for (int i = 1; i < array.length; i++) {
if (array[i].getPrice() > x) {
x = array[i].getPrice();
}
}
return x;
}
//implement the static method bestSeller in the Book class,
//which accepts an array of Books and return the best selling Book
public static Book bestSeller(Book[] books) {
Book bestSeller = books[0];
for(int i = 1; i < books.length; i++){
if(bestSeller.numberSold < books[i].numberSold)
bestSeller = books[i];
}
return bestSeller;
}
public double getPrice() {
return price;
}
public void setPrice(double newPrice) {
price = newPrice;
}
public String getName() {
return this.name;
}
public void setName(String newName) {
name = newName;
}
/*
*
* Element k of the returned array
//contains the single letter at position k of str. For example,
//getSingleLetters ("cat") returns the array {"c", "a", "t"}.
*
* */
}
public class Library{
private String libraryName;
private Book [] list;
public Library (String name, Book [] books){
//complete this constructor
}
//Write the instance method BestSeller including its method
//header, which returns the name of the bestselling book.
//Return the first of the best selling books if there's more than one.
public Book bestSeller(){
return Book.bestSeller(list);
}
//Write the instance method doublePrice, which doubles the
//price of each book in list.
public void doublePrice(){
for(int i = 0; i < list.length; i++){
Book book = list[i];
book.setPrice(book.getPrice() * 2);
}
}
//Write the static method longestWord which accepts a string
//array and returns the length of the longest word in the array.
//String [] strings
//return int;
public static int longestWord(String [] strings){
int longest = 0;
for(int i = 0; i < strings.length; i++){
String word = strings[i];
if(word.length() > longest){
longest = word.length();
}
}
return longest;
}
//Write the static method gotSingleLetters which accepts a String str
//and returns a string array. Each of these strings
//consists of a single letter from str.
//return string[]
public static String [] getSingleLetters(String str){
String[] allLetters = new String[str.length()];//capacity; number of things that can fit
for(int i = 0; i < str.length(); i++){
allLetters[i] = str.substring(i, i + 1);
}
/*
String s = "cat";
s.substring(1, 2);
*/
return allLetters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment