Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to show the maximum and the minimum in array...
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Mushfiqur Rahman Sajid
How to show the maximum and the minimum in array...
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners


I have a bookstore application, but I cannot show the maximum and the minimum price of the book. Please see my codes below.

import java.util.*;
public class Book{
public static void main(String args[]){
int numOfBook;
double totalCost = 0;
String expBookTitle = "";
double expBookPrice = 0;
String cheapBookTitle = "";
double cheapBookPrice = 0;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to SAJID's Book Shop");
System.out.print("How many book would you like to store: ");
numOfBook = input.nextInt();
String[] book = new String[numOfBook];
double[] bookPrice = new double[numOfBook];
for(int i=0;i<book.length;i++)
{
System.out.print("Please enter book name: ");
book[i] = input.next();
System.out.print("Please enter book price: ");
bookPrice[i] = input.nextDouble();
System.out.println();
totalCost += bookPrice[i];
if(bookPrice[i] == expBookPrice)
{
expBookPrice = bookPrice[i];
expBookTitle = book[i];
System.out.println("Expensive book price: " + expBookPrice);
System.out.println("Expensive book title: " + expBookTitle);
if(bookPrice[i] <= expBookPrice)
{
cheapBookPrice = bookPrice[i];
cheapBookTitle = book[i];
System.out.println("Cheap book price: " + cheapBookPrice);
System.out.println("Cheap book title: " + cheapBookTitle);
}
}
}
}
}

Advertisement
View Answers

June 7, 2012 at 3:41 PM


The given code accepts the user to enter the number of books. According to that, user enter the name and book and price. These values will get stored into respective arrays. Then it will determine the expensive book and cheap book. Here is the code:

import java.util.*;
     public class Book{
        public static void main(String args[]){
            int numOfBook;
            double totalCost = 0;
            String expBookTitle = "";
            double expBookPrice = 0;
            String cheapBookTitle = "";
            double cheapBookPrice = 0;
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to SAJID's Book Shop");
            System.out.print("How many book would you like to store: ");
            numOfBook = input.nextInt();
            String[] book = new String[numOfBook];
            double[] bookPrice = new double[numOfBook];
            for(int i=0;i<book.length;i++)
            {
                System.out.print("Please enter book name: ");
                book[i] = input.next();
                System.out.print("Please enter book price: ");
                bookPrice[i] = input.nextDouble();
                System.out.println();
            }
            double max = bookPrice[0];
            double min = bookPrice[0];
            int a=0,b=0;
            for (int i=1; i<bookPrice.length; i++) {
                if (bookPrice[i] > max) {
                max = bookPrice[i];
                a=i;
                }
                if (bookPrice[i]< min) {
                min = bookPrice[i];
                b=i;
                }
            }
            System.out.println("Expensive book price: " + max);
            System.out.println("Expensive book title: " + book[a]);

            System.out.println();

            System.out.println("Cheap book price: " + min);
            System.out.println("Cheap book title: " + book[b]);
            }
            }


Related Tutorials/Questions & Answers:
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book... to SAJID's Book Shop"); System.out.print("How many book would you like to store
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book... to SAJID's Book Shop"); System.out.print("How many book would you like to store
Advertisements
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
How do I use JGraph to create a MST(minimum spanning tree) using java?
How do I use JGraph to create a MST(minimum spanning tree) using java?  How do I use JGraph to create a MST(minimum spanning tree) using java? thanks for the help
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
how to show popup in javascript
how to show popup in javascript  How to show popup in javascript
how to show data in database ?
how to show data in database ?  how to show the data in the database to the user in the textbox so that user can modify it and save it again
HOW TO SHOW TIME SERIES
HOW TO SHOW TIME SERIES  hi................ i require a small code in java swings to display the output as time series. Can u plz help???????? Reply....... thanks in advance
How you will Show unique records.
How you will Show unique records.  How you will Show unique records.   Hi, Here is the query which shows the unique records- SELECT DISTINCT columnname FROM tablename; Thanks
How to declare String array in Java?
Following example will show you how to declare string array in java. There are many ways to declare an array and initialize them. We can use 'new' keyword to declare an array. However, an array can also be declared without
Find max and min value from Arraylist
to find the maximum and minimum value element from the ArrayList. Java provides direct methods to get maximum and minimum value from any collection class i.e...); } } Output: Minimum value is = 15 Maximum value is = 550
How to: generic array creation
How to: generic array creation  How to: generic array creation
how to show random image in ASP.net?
how to show random image in ASP.net?  hello bros i saw in many websites..there is a programming of random image changing.... i want to use this in my ASP.NET websites, can any one suggest me code or any kind of help..so that i
What is the maximum size of a file that can be uploaded using PHP and how can we change this?
What is the maximum size of a file that can be uploaded using PHP and how can we change this?  What is the maximum size of a file that can be uploaded using PHP and how can we change
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length  how to move curosr from one text field to another automatically when the first textfield reaches its maximum
JavaScript Array Contains Method
JavaScript Array Contains Method       In the given JavaScript array example, we are going to show how an array contains method(). As you already know we required the property
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
how to show required fields indicate as * in struts2
how to show required fields indicate as * in struts2  how to show required fields indicate as * in struts2
How to Prepend Array in PHP
How to Prepend Array in PHP  hi, How can i prepand array in PHP. Please suggest any example of prepand array in PHP program. Thanks,   Hi, The PHP prepand array function in used before adding any elements
SQL Aggregate Queries
; SQL Aggregate Queries show you the list of Aggregate Function that include Sum, Count, Average, Maximum, Minimum  etc. Understand with Example... the maximum records value from table stu.  mysql>
how to use button in array
how to use button in array  plz tell me how to code for button in array
How you will Show all data from a table.
How you will Show all data from a table.   How you will Show all data from a table.   Hi, following query is used to show all the data of the table- SELECT * FROM tablename; Thanks
Advertisements
 

 

 

DMCA.com