ahhhh ok I understand now, so it select the first hidden one! thanks for clarifying that! sorry I think I still have troubles getting this idea of multi filtering in my head. I interpreted that as selecting the hidden divs and then the first one, whether it was hiddin or not
Violet_82 89 Posting Whiz in Training
Violet_82 89 Posting Whiz in Training
Hi chaps, I have a problem with my dell xps 17 lappy, the connector for the main is really dodgy inside the laptop itself therefore charging it is a nightmare. I was wondering if there is such a thing as a USB main charger that can be plugged in to a usb port of the lappy rather than to the usual power cable connector and charge the battery (I assume the computer needs to be on for it to work).
Have you ever heard anything like that?
thanks
Violet_82 89 Posting Whiz in Training
Hi there, I was wondering if anybody can help me understanding this code please - well it's more about the usage of :first really):
<!DOCTYPE html>
<html>
<head>
<style>
span { color:red; cursor:pointer; }
div { margin:3px; width:80px; display:none;
height:80px; float:left; }
div#one { background:#f00; }
div#two { background:#0f0; }
div#three { background:#00f; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<span>Click here...</span>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<script>
$(document.body).click(function () {
$("div:hidden:first").fadeIn("slow");
});
</script>
</body>
</html>
Now, the purpose of this code - taken from http://api.jquery.com/fadeIn/ - is to reveal a square each time you click on the link. What I am not clear about is how the selector works: $("div:hidden:first").
this selects every first hidden div, but the :first filter selects only one element, so I assume it will select always the first div because it is the first one! That said if I remove it to obtain $("div:hidden")
then the 3 divs appear at the same time when you click the link. SO my question is, how does the :first filter manage to select only one div each time the link is selected?
thanks
Violet_82 89 Posting Whiz in Training
ah sorry, I forgot to print the message when the function is first called, plus a few more changes, now it works!!! aaaaaaaaaaaahhh! I can't believe it, it wooooorksss!!
Right, big big thanks to all you guys who've contributed to this thread, thanks a lot for your help and patience!
I have learned a lot from this exercise, and I will make absolutely sure I will come up with a very good pseudocode before I start coding and not the other way around (which is what I have essentially done this time).
Here is the final version of the program in case anybody wants to use it (I doubt it) or use it like a starting point (a bit more likely!)
I suppose I have looked at it so many times that now I miss the obvious!
//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
private final int NUMBER_OF_SEATS = 10;
private boolean[] seats;
//constructor
public Seats(){
//initializing array
seats = new boolean[ NUMBER_OF_SEATS ];
//set values to false - doesn't it do automatically?
for( int counter = 0; counter < seats.length; counter++ ){
seats[ counter ] = false;
}//end of loop
}//end of constructor
public int assignSeat( int type){
int seatPlace = -1; //default seatPlace value
int firstI = 0; // firstIndex
int lastI = 4;
if ( type == 2 ){
firstI += 5;//second index
lastI += 5;
}
for ( int i = firstI; i <= lastI; i++ ){ …
Violet_82 89 Posting Whiz in Training
gosh, this exercise is a nightmare...I have included the function but the program seems to ignore it completely for whatever reason. Basically in Seats.java the function returns true to the function caller if all the seats are taken and a few ifs statements handle this till another if terminates the application. I don't use the false value returned by the function:
...
public boolean planeIsFull(){
int counter = 0;//holding the number of filled seats
for( int i = 0; i < seats.length; i++ ){
if( seats[ i ] == true ){//if all the seats are taken
counter++;//increase counter
}
}//end of loop
if( counter == NUMBER_OF_SEATS ){//if counter == 10 then the plane is full
return true;
}
else{
return false;
}
}//end of planeIsFull
}//end of class
and here's the SeatsTest.java
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
//constants
static final int FIRST_CLASS_SELECTION = 1;//first class
static final int SECOND_CLASS_SELECTION = 2;//second class
public static void main( String[] args ){
Seats seatingPlan = new Seats();
int planeLeave = 0; //flag if users don't want to book more seats
//int firstCounter = 0;//keep the seat count
//int secondCounter = 0;
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//flag determines whether seats have been assigned or not
int seatAssigned = 0;
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display …
Violet_82 89 Posting Whiz in Training
ok will try that. I am just thinking, if the function returns true (all seats allocated) or false (some free) what would I do with those values returned? I mean with the true value it's pretty straightforward in that, if the value is true then I will end the program, but if it returns false, what do I do with that value? DO I have to use it for something?
Violet_82 89 Posting Whiz in Training
oh I see, I am such a donkey!!
ok I have amended that and almost everything seems to be working ok. I can hear you says "yay!" but no, I had the insane idea og handling also the situation when the plane is full, so I added some more code (and modified the existing one too) so that, in theory at least, when the plane is full a message saying "Sorry the flight is full, the next one leaves in 3hrs" prints out. Well, good idea isn't it>? Except that it is not working the way it should. Here's what I have done:
1)I have added 2 variables (you must have seen these in the code they were commented out):
int firstCounter = 0;//keep the seat count
int secondCounter = 0;
Then in the while loop at the top straight after the function call I have a switch statement:
seatAssigned = seatingPlan.assignSeat( classRequested );
if( seatAssigned != -1 ){
switch( classRequested ){
case 1:
firstCounter++;
break;
case 2:
secondCounter++;
break;
}
}
In the switch( classRequested ){case 1:
if the user selects 'y' I increase the secondClass counter because I call the function with a parameter of 2 and finally an if statement determines whether the plane is full or not:
if( ( firstCounter == 5 ) && ( secondCounter == 5) ){//the flight is full
System.out.println( "\nSorry the flight is full, the next one leaves in 3hrs\n" );
break;//leaves the while …
Violet_82 89 Posting Whiz in Training
ok no worries. So, I compiled and run everything. The good news is that I managed to remove all the compiling errors and the program even runs! The bad news is that it doest't run the way it should, I must have made some logic error somewhere, and I think I know where, in the switch satement:
switch( classRequested ){
case 1:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( SECOND_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
//break;
case 2:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s ", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( FIRST_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
Right, userChoice
in userChoice = String.format( "\nYou have selected %s", action );
is declared as a string and userInput = userChoice.charAt(0);
(userInput is a char) simply convert a string to …
Violet_82 89 Posting Whiz in Training
it's fine I can do that, but then if I call this variable userInput how am I going to call the other one that is supposed to convert the userInput from string to char?
Violet_82 89 Posting Whiz in Training
ok will have another variable to convert the string to character. As for the action
variable, I am reusing it yes, but for the same purpose (although with different values): the first time it takes 1 or 2 as input the second time 'y' or 'n'
thanks
Violet_82 89 Posting Whiz in Training
thanks for the info bguild. I had a look at charAt(0)
and I think that will do for me. One thing I'd like to mention though: following what you said before I have realized that some of the variables can be reused, like action
, so I have this situation:
if( seatAssigned == -1 ){
String userChoice;//contains y (assign seat in the other class )or n (dont' assign seat in the other class)
//switch statement to assign seats of the other class if one is full
switch( classRequested ){
case 1:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s", action );
JOptionPane.showMessageDialog( null, userChoice );
if( userChoice == 'y' ){
this line userChoice = String.format( "\nYou have selected %s", action );
gets the user input which at the moment is still a string, so I need to convert it into a char so if I use charAt(0)
, do I have to create another variable likechar theInput = userChoice.charAt(0);
? This is assuming that userChoice is a String type.
But, if I declare char userChoice
instead of String userChoice
, would it be ok to use a cast operator and change the type of action
from String to char oon the fly, something like userChoice = String.format( "\nYou have selected %s", (char) action );
I don't …
Violet_82 89 Posting Whiz in Training
ah ok so it takes just attributes and not tags, ok thanks for this (no pun intended!)
Violet_82 89 Posting Whiz in Training
crystal clear, thanks for the explanation.
Violet_82 89 Posting Whiz in Training
thanks, that's much clearer now, so can this be used with anything then or just ids? Like the above example shows id.title
, so can I use this.anyHTMLtag
, this.class
, etc or is there some kind of rule that limits its usage? I had a look around on the net but to be honest with you I didn't find anything that helpful or easy to understand
thanks
Violet_82 89 Posting Whiz in Training
Chaps,
I was wondering what this.id (and for that matter this.whatever) is in jquery and how to use it. A few quick examples are here:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<img title="hat.gif"/>
<script>
$("img").attr("src", function() {
return "/resources/" + this.title;
});
</script>
</body>
</html>
and another one:
<!DOCTYPE html>
<html>
<head>
<style>
div { color:blue; }
span { color:red; }
b { font-weight:bolder; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>Zero-th <span></span></div>
<div>First <span></span></div>
<div>Second <span></span></div>
<script>
$("div").attr("id", function (arr) {
return "div-id" + arr;
})
.each(function () {
$("span", this).html("(ID = '<b>" + this.id + "</b>')");
});
</script>
</body>
</html>
thanks
Violet_82 89 Posting Whiz in Training
Hi all, I am slightly confused about passing functions to the addClass method. This code is taken from the jquery.com site http://api.jquery.com/addClass/:
<!DOCTYPE html>
<html>
<head>
<style>
div { background: white; }
.red { background: red; }
.red.green { background: green; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>This div should be white</div>
<div class="red">This div will be green because it now has the "green" and "red" classes.
It would be red if the addClass function failed.</div>
<div>This div should be white</div>
<p>There are zero green divs</p>
<script>
$("div").addClass(function(index, currentClass) {
var addedClass;
if ( currentClass === "red" ) {
addedClass = "green";
$("p").text("There is one green div");
}
return addedClass;
});
</script>
</body>
</html>
Couple of points:
1)how is the script called? There is no call, does it run on document load?
2)addClass(function(index, currentClass) {
: what are these 2 parameters index and currentClass, where do they come from?
3)return addedClass;
if they add this, does it mean that the whole expression gets the value of addedClass
?
thanks
Violet_82 89 Posting Whiz in Training
Sorry I, will change the name to something else then, perhaps like moreSeat
. fullClass
is a char because it is storing the user's choice ('y' for yes and 'n' for no). I didn't want to use an int not to generate confusion with the first user inputs of 1 and 2. fullClass
is used only if users want more seats. if I use a char it complains that I can't compare a string to a char so I will have to find a way to convert a string to a char, is it possible?
thanks
Violet_82 89 Posting Whiz in Training
ah, problem with this fragment:
char fullClass;
//switch statement to assign seats of the other class if one is full
switch( classRequested ){
case 1:
//ask user to change class
fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no.", classRequested );
JOptionPane.showMessageDialog( null, fullClass );
if( fullClass == 'y' ){
assignSeat( SECOND_CLASS_SELECTION );
}
else{
The compiler is complaining about comparing a char (fullClass) with a string and a bunch of other errors that I am not entirely sure I understand:
antobbo@antobbo-xps17-ubuntu:~/Documents/dell xps/My documents/java/tests/airline$ javac *.java
SeatsTest.java:44: error: incompatible types
fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no.", classRequested );
^
required: char
found: String
SeatsTest.java:47: error: cannot find symbol
assignSeat( SECOND_CLASS_SELECTION );
^
symbol: method assignSeat(int)
location: class SeatsTest
SeatsTest.java:56: error: incompatible types
fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in first class? Type y for yes and n for no.", classRequested );
^
required: char
found: String
SeatsTest.java:59: error: cannot find symbol
assignSeat( FIRST_CLASS_SELECTION );
^
symbol: method assignSeat(int)
location: class SeatsTest
4 errors
antobbo@antobbo-xps17-ubuntu:~/Documents/dell xps/My documents/java/tests/airline$
For the char problem, even if I replace the char with a string it keeps complaining.
Any idea at all?
Violet_82 89 Posting Whiz in Training
Ok it makes sense, and well in fairness I must admit that it often happened to me to leave some code aside for a while and then come back to it after a week or so, and I had absolutely no idea what was going in there, so you're definitely right. I will amend the code:-)!
Violet_82 89 Posting Whiz in Training
Oh I see, basicallyassignSeat( 1 );
and assignSeat( 2 );
force the seat assignment for the other class if the seat returns -1: for example, if the user select 1 at the prompt and the seat returned a value of -1 we know that first class is full and therefore I force assignSeat()
to assign a seat in the second class by using assignSeat( 2 );
. I would have thought it was clear enough, but if you think that is a concern I will then have 2 constants, FIRST_CLASS = 1;
as you suggested and SECOND_CLASS = 2;
I will try to fix the errors in the compiler (some are quite obscure to be honest) and I will post again.
thanks for now : - )
Violet_82 89 Posting Whiz in Training
thanks, hopefully this time it should be better (I am now dealing with a few errors returned by the compiler) but I wonder whether the logic is ok now. Here's the new assignSeat()
public int assignSeat( int type){
int seatPlace = -1; //default seatPlace value
int firstI = 0; // firstIndex
int lastI = 4;
if ( type == 2 ){
firstI += 5;//second index
lastI += 5;
}
for ( int i = firstI; i <= lastI; i++ ){
if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
seats[i] = true;
if ( type == 1 ){
System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
}
else if ( type == 2 ){
System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
}
return seatPlace = i; // flag sucessfully seat assignment
}
}
// if the method reaches this point, it means no seats were still available,
return seatPlace;
}//end of assignSeats
and here's the test file:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
public static void main( String[] args ){
Seats seatingPlan = new Seats();
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//Integer.parseInt( action );
//flag determines whether seats have been assigned or not
int seatAssigned …
Violet_82 89 Posting Whiz in Training
That's what the classRequested variable is for!
oops...I think I have missed the obvious here...apologies : - )! But I thought that the classRequest
variable returns the a flag which says whether the assignment is successful or not and then I can use the action
variable to determine the which class we are booking the seats for and I can use that to determine if a class is full
Violet_82 89 Posting Whiz in Training
uhm, I have relooked at some of the posts and if I adapated my code to that, maybe it will solve my problem, like this:
public int assignSeat( int type){
int seatPlace = 0; //default seatPlace value
int firstI = 0; // firstIndex
int lastI = 0;
if( type == 1 ){
firstI = 0; // firstIndex
lastI = 4;
seatPlace = -1;//for 1st class
}
if ( type == 2 ){
firstI += 5;//second index
lastI += 5;
seatPlace = -2;//for 1st class
}
for ( int i = firstI; i <= lastI; i++ ){
if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
seats[i] = true;
if ( type == 1 ){
System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
}
else if ( type == 2 ){
System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
}
return seatPlace = i; // flag sucessfully seat assignment
}
}
// if the method reaches this point, it means no seats were still available,
return seatPlace;
}//end of assignSeats
Like this depending on which class I assign seats for, I have a value returned to the caller identifying a specific class which I can then target.
Violet_82 89 Posting Whiz in Training
ok will amend that, but I still have the problem described above, in that I need to determine which class (1st or 2nd) is full, so somehow I need not only -1 to say that all the seats have been assigned but another value to flag the type of class if it makes sense
Violet_82 89 Posting Whiz in Training
thanks both for the suggestion. I can see you both suggested to use a sentinel value of -1. I have implemented that but unfortunately it doesn't help me, let me explain why.
Take the assignSeat() in Seats.java:
public boolean assignSeat( int type){
int seatPlace = -1; //default seatPlace value
int firstI = 0; // firstIndex
int lastI = 4;
if ( type == 2 ){
firstI += 5;//second index
lastI += 5;
}
for ( int i = firstI; i <= lastI; i++ ){
if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
seats[i] = true;
if ( type == 1 ){
System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
}
else if ( type == 2 ){
System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
}
return seatPlace = i; // flag sucessfully seat assignment
}
}
// if the method reaches this point, it means no seats were still available,
return seatPlace;
}//end of assignSeats
So the seatPlace variable is returned back to the caller in the SeatsTest.java:seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));
Now, here's the problem: if the value of -1 is returned, it signifies that the seat assignment wasn't successful but it doesn't say which class (1st or 2nd) we were trying to assign the seats for, and in my SeatsTest.java I need to …
Violet_82 89 Posting Whiz in Training
right ok, I have changed the program quite a lot and took the approach that stultuske has suggested. So far so good, but I have now a few questions (yes I know, surprise surprise!). Let's have a quick look at the code first:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
public static void main( String[] args ){
Seats seatingPlan = new Seats();
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//Integer.parseInt( action );
//flag determines whether seats have been assigned or not
boolean seatAssigned = false;
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
while( Integer.parseInt(action )!= 0){
seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));
if( !seatAssigned ){//no seats available
//if no seat for 1st or 2nd class need to ask the user if he's happy to get a seat in other class
}
//ask for input again
action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//create message
message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
}
}
}
and then
//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
private final int NUMBER_OF_SEATS = 10;
private boolean[] seats;
//constructor
public Seats(){
//initializing array
seats = new boolean[ NUMBER_OF_SEATS ];
//set values to …
Violet_82 89 Posting Whiz in Training
ok I see what you mean so that function takes care of all the seat assignment, definitely much more efficient than mine I have to admit...Now, what's with all those flags then?
As I said
3) not quite clear with this:
if takenFlag = 2 OR takenFlag = 4
display boarding pass
else if takenFlag = 2
// this means, you asked economic, but wasn't any left
if the flag is equal to 2 means that you have assigned a seat in economic class, but then you say that if takenFlag =2 there are no seats in economic?
Same story when takenFlag is equal to 3: that variable is set to 3 just after the user has chosen 2nd class so how do I relate that to the fact that there are no seats available in second class?
I don't quite understand the way they are used to flag whether the seats are assigned or not
thanks
Violet_82 89 Posting Whiz in Training
why are you generating seats - at random - ?
well becasue the exercise doesn's say not to, and having done random numbers recently I thought it would be a good idea to do that. I thought about efficiency to be honest, and I didn't think it was such a big deal for such a small program. And also, I couldnt' think of another way to assign seats. That said, happy not to use random number generators if you think isn't necessary.
Now, to go back at your pseudocode, got a couple of questions:
1)not entirely sure what takenFlag does, what is that flagging? you set takenFlag = 0, then takenFlag=1 all the way up to 4
2)if I don't assign seats with a random number generator, how do I actually do it?
3) not quite clear with this:
if takenFlag = 2 OR takenFlag = 4
display boarding pass
else if takenFlag = 2
// this means, you asked economic, but wasn't any left
if the flag is equal to 2 means that you have assigned a seat in economic class, but then you say that if takenFlag =2
there are no seats in economic?
Same story when takenFlag is equal to 3: that variable is set to 3 just after the user has chosen 2nd class so how do I relate that to the fact that there are no seats available in second class?
thanks
Violet_82 89 Posting Whiz in Training
ok chaps, I appreciate you have diverging opinions and I can't really butt in because I know only very little. SO why don' we start just from scratch again? I think I have made a very big mistake at the beginning, which is not producing any pseudo code but start the development straight away.
So here's some pseudocode, maybe that will help me. Once I get that right I suppose it shouldn't be too bad to build the program. Needless to say my pseudocode has some issues, as in I am doing a few things twice. I know bguild doestn' like that so perhaps you guys can help me with the pseudocode first and then I can move on to build the application?
Please type 1 or 2 to choose the class, 0 to quit
if 1
generate seat at random from values smaller than the last index of the last seat of the section (10-5 = 5 so index 4)
else if 2
generate seat at random from values bigger than index4 of the array and smaller than the last index of the last seat of the section array.length - 1
check whether seat has been already assigned
if so generate seat again till you find a seat that hasn't been assigned to
if not assign the seat and display boarding pass
if first class has no seats and second has
ask user if he/she wants to have a seat in the second class
if no, display …
Violet_82 89 Posting Whiz in Training
thanks bguild, I appreciate that planning for future is good, to be honest it didn't really crossed
my mind when I started the exercise, because this forward-thinking wasn't required by the exercise.
Your seats array is a boolean[]. You shouldn't be dividing its elements by 2; division is only for numbers
This is not correct, I am not diving the elements of the array by tow, but the lenght of the array which is an integer
Ok, so let's try to then put things in order here.
Constants: we want 3 constants:
private int NUMBER_OF_SEATS = 10;
I think you said it isn't a good idea to have it as finalprivate int FIRST_CLASS_SEATS = 5;
private int SECOND_CLASS_SEATS = ?;
what should be the value of this constant then?
I think you should stop doing things twice.
Ok so you propose only 2 methods methods sectionStart(int seatClass)
and sectionEnd(int seatClass)
,that do everything (check if there are seats available, choose a seat in the relevant section and check for the unassignedseat in that specific section). These 2 methods will effectively replace checkSeat()
and quite a bit of the code in assignSeats()
- I suppose I will need to keepgenerateClassSeats()
because it seems to be doing its job ok although I will need to modify it slightly when I add the constants
Your checkSeat is not good. There are far better strategies available for solving your problem.
What would be a better way …
Violet_82 89 Posting Whiz in Training
bguild, thanks for getting back to me. I have made some changes that seems to address the above issues.
I post the whole file because it is easier to see the changes since there are quite a few:
//ex 7.19 p 333 deitel and deitel
//Seats.java
import java.util.Random;
public class Seats{
private int final NUMBER_OF_SEATS = 10;
private boolean[] seats;
private int firstSeat;
private int secondSeat;
private int firstClassCounter;//counter for first class
private int secondClassCounter//counter for second class
Random randomNumbers = new Random();
//constructor
public Seats(){
//initializing array
seats = new boolean[ NUMBER_OF_SEATS ];
//set values to false - doesn't it do automatically?
for( int counter = 0; counter < seats.length; counter++ ){
seats[ counter ] = false;
}//end of loop
}//end of constructor
public void assignSeats( int seatClass ){
firstSeat = generateClassSeats( seatClass );//pass the user choice
//check that I don't reassign the seat
if((firstClassCounter <= seats.lenght / 2) || (secondClassCounter <= seats.lenght / 2)){
firstSeat = checkSeat( firstSeat );
//assign the seat
seats[ firstSeat ] = true;
}
//check the status of first and second class seats
if (firstSeat < seats[ seats.lenght-1 ] / 2){//if firstSeat < 4
firstClassCounter++;//increase first class counter
}
else if(firstSeat > seats[ seats.lenght-1 ] / 2){//if firstSeat > 4
secondClassCounter++;//increase second class counter
}
//theSeat = seats[ firstSeat ];
//determine 1st or 2nd class
if( seatClass == 1 ){
System.out.printf("Your seat number is %d and it is in first class", firstSeat + 1 );
}
else if( seatClass == 2 ){ …
Violet_82 89 Posting Whiz in Training
Hi all, I'd like to clarify something here please, the inconsistent behaviour of browsers with regards to the pseudo class :visited. I had done quite a lot of research into it ont he web and it seems to me that every browser does something different. I appreciate this has a lot to do with security but in some instances it seems a bit awkward. Let me give you a few examples. I am working on a website at the moment, which is optimized to display in mobile browsers too. If I leave the defaults pseudo classes (meaning I don't change them at all and leave the defaul colour for links) I noticed that every browser seems to work fine, in that an unvisited link is blue and turns purple when visited. But, the moment I change those properties (and change them in the correct order of course) things go bananas. Take this fragment of code:
/*STYLED LINKS*/
a.myLink:link{
color:#c73085;
}
a.myLink:visited{
color:#08dff0;
}
a.myLink:hover{
color:#080808;
}
a.myLink:active{
color:#c73085;
}
/*STYLED LINKS*/
When this is applied to my css, only firefox, IE and opera seems to fully support it.The colour of the links changes ok the problem is only with visited links. Safari and Chrome behave in a very interesting way: if I click on the link, the colour doesn't change to visited and stays the same, but when I hover with the mouse on the clicked link, it changes to the right, visited colour. Even if I change …
Violet_82 89 Posting Whiz in Training
I see what you mean now. I am thinking though I might not need constants for first class seats and second class seats because I can determine them with seats[ seats.lenght-1 ] / 2
if I need to.
Now, I have made some changes following your feedback, but I have a few problems:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
public static void main( String[] args ){
Seats seatingPlan = new Seats();
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
while(action != 0){
seatingPlan.assignFirstSeats( action );
//ask for input again
action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//create message
message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
}
System.out.print("You are exiting the application. Thank you.\n");
}
}
Here I have removed completely the code determining whether the user's input is 1 or 2, I will take care of it somewhere else. As long as it is not 0 then we are fine.
The second file is this:
//ex 7.19 p 333 deitel and deitel
//Seats.java
import java.util.Random;
public class Seats{
private int final NUMBER_OF_SEATS = 10;
private boolean[] seats;
private int firstSeat;
private int …
Violet_82 89 Posting Whiz in Training
LastMitch, thanks for that. The site doesn't really have an issue as such I am just planning a redesing/redevelopment. However, if you think posting some of the code will help, then here it is.
1)Home pictures that need resizing (as said assign width:100px/max-width:100px
)
<div class="home_page_pic"><img style="display: none;" alt="" src="images/church_full_1.jpg"></img><img style="display: none;" alt="" src="images/city_full_2.jpg"></img><img style="display: none;" alt="" src="images/faith_full_1.jpg"></img><img style="display: none;" alt="" src="images/flower_full_2.jpg"></img><img style="display: block; opacity: 0.878155;" alt="" src="images/gloom_full_2.jpg"></img></div>
<!--END OF home_page_pic -->
<div class="footer"></div>
<!-- END OF footer -->
For the thumbnails, here's an example: I guess I will do the same thing with them:
<div class="thumbnail"><a class="full_image" href="javascript:void(0);"><img onclick="change_image('big_image_1')" style="border:0" alt="" src="images/animal_thumb_1.jpg"></img></a></div>
<!-- END OF thumbnail 1-->
<div class="thumbnail"><a class="full_image" href="javascript:void(0);"></a></div>
<!-- END OF thumbnail 2-->
<div class="thumbnail"></div>
<!-- END OF thumbnail 3-->
<div class="thumbnail"></div>
2)For the comment, I am not sure what to use to enable comments for only selected pictures. As said, I want users to be able to comment on some pictures,which will be in a separate section of the site, a blog-like page
3)Hand code or worpress? I generally like handcoding, but I don't know of any plug in that could allow me to do what wordpress does
4)Linking to desktop site: how do you achieve that?
Violet_82 89 Posting Whiz in Training
bguild, thanks. I think I will have to spend sometime on this program by the look of it:-)!
I am working to simplify the code as per your suggestion. I have a few questions:
-Even worse, you provide a NUMBER_OF_SEATS constant that someone
I appreciate this, but somehow I have to set the size of the array, and if not like a constant I am not sure how to do it. The seat capacity needs to be specified by the program and not entered by the user, so not sure how I would set it without using a constant? I can't use the array lenght because the array hasn't been declared yet
Instead of generateFirstClassSeats() and generateSecondClassSeats(), notice what happens if you have generateSeats(seatClass)
Point taken. I am in the process of making changes: To assign seats something like this:
public void assignSeats( int seatClass ){
firstSeat = generateClassSeats(seatClass);
//checkSeat();
theSeat = seats[ firstSeat ];
//determine 1st or 2nd class
if( firstSeat <= seats[ seats.lenght-1 ] / 2 ){
...
I haven't finished the assignSeats method yet but this gives you an idea I suppose. the condition of the if statement makes sure that whatever the lenght of the array, I can always safely divide it in 2 and obtain half seats for first class and half for the second class. What do you think?
To generate seats, as you have mentioned:
//generate class seats
private int generateClassSeats(int classType){
//generate random number …
Violet_82 89 Posting Whiz in Training
thanks, I have done some more work on it, not sure if it makes sense though:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
public static void main( String[] args ){
Seats seatingPlan = new Seats();
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
while(action != 0){
seatingPlan.assignSeats( action );
//ask for input again
action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//create message
message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
}
}
}
and the other file:
//ex 7.19 p 333 deitel and deitel
//Seats.java
import java.util.Random;
public class Seats{
private int final NUMBER_OF_SEATS = 10;
private int final PASSENGER_CLASSES = NUMBER_OF_SEATS / 2;//to get first class seats(first 5 seats of the array) and second class (last 5 seats of array)
private boolean[] seats;
private int firstSeat;
private int secondSeat;
//private boolean seatAvailable;
Random randomNumbers = new Random();
//constructor
public Seats(){
//initializing array
seats = new boolean[ NUMBER_OF_SEATS ];
//set values to false - doesn't it do automatically?
for( int counter = 0; counter < seats.length; counter++ ){
seats[ counter ] = false;
}//end of loop
}//end of constructor
public void assignSeats( int seatClass ){
if( …
Violet_82 89 Posting Whiz in Training
Chaps, I am thinking to redesign and redevelop my site, http://www.antonioborrillo.co.uk/ I want to get rid of those hideous buttons and make
it mobile friendly, add extra info and have a page where people can leave comments on some of
the pix. Now first thing: mobile version. I will go for a fluid layout and with media queries
I will use these breakpoints http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
to make sure that it is displayed ok on every device. There are some issues off the top of my head,
but obviously if you can think of any other please let me know. Here's a list, I'd appreciate
some advice please as to how to tackle them
The issues:
1)display pictures on mobile. I'd like to keep the existing sizes as they are (thumbnails and
full size pics), but I will use
max-width=100% and width:100%; to make sure the photos are resized when the broser resize; For
thumbnails shouldn't be a problem at all, but for full width pictures I am not entirely sure
what to do. I am thinking to scrap my code ad use a plug in to make sure that not only it works
perfectly on as many devices as possible (but I think there are no major problem with my code),
and that the pix are resized correctly on every device.
In terms of how the information is presented
( thumbnails that …
Violet_82 89 Posting Whiz in Training
thanks stultuske, I forgot to mention that my choice are pretty limited in that I haven't done subclasses as yet so I don't quite know how to use them as yet, and the exercise is asking to use a 1 dimensional array. I appreciate you guys want me to follow the good practice, but I have only got as far as arrays (chapeter 7 of the book), so even if there are probably many many ways to solve this exercise I don't have many options, that's another reason why I wanted to stick to classes and not enum
Violet_82 89 Posting Whiz in Training
ok sorry, I thought enum and classes were mutually exclusive, in that if I use an enum here I won't need to use a class, but I seem to understand that's not the case. Well, i will see how it goes and then post back. I think the exercise wants me to use only one array
Violet_82 89 Posting Whiz in Training
ah, I see everybody likes enum : - ), unfortunately I don't know enough about the language to provide a valid argument not to use them, so if you guys say they are useful then, so it is. The thing is, if I use an enum in the exercise abovem then I won't be able to use a class I believe, and I have always thought that learning how to use classes is somehow more important. At least this is one of the things I remember from when I was doing C++
@Starstreak: yes you're right, but rest assured that my ariline booking system won't be used by anybody else other than myself! : - )
Violet_82 89 Posting Whiz in Training
thanks bguild. I don't feel confortable using enum, for some reasons I don't like them, so if I can do without I feel happier.
About the issue of one user taking all the seats, I think I could sort this out by enclosing all my code in a while loop, something that allows the program to quit when you input a specific value, something like "q" for the sake of argument, so that the options at the beginning will be: 'press 1 for 1st class, 2 for 2nd and q to quit the application' and do this all over again till the seats run out. This means I will lose the two for loops for( int counter = 0; counter < seats.length / 2; counter++ )
because I will have to assign a seat for each user therefore resolving the issue - well the confusion perhaps - with seats.length / 2
.
Not entirely sure what you mean by using more constants, I thought the only one I needed was the one indicating the element of the arrays, 10. The exercise says to use only one array so I thought I will do with just seats[]
. If I use FIRST
and SECOND
then I will have to use 2 arrays, or I think it might be more complicated.
I kind of like the idea of randomness, not sure why, it's just cool, but if it turns out to be too complicated I will abandon it.
To make sure that …
Violet_82 89 Posting Whiz in Training
hi, thanks for deleting it, it did happen before, I think last week and again I definitely didn't submit it twice. I will let you know if it happens again.
cheers
Violet_82 89 Posting Whiz in Training
Chaps I think there might be an issue here, I have just posted something http://www.daniweb.com/software-development/java/threads/449524/building-a-simple-airline-reservation-system-exercise and it looks like it posted it twice. I am fairly sure I didn't press submit twice, so I am not sure what's going on.
thanks
Violet_82 89 Posting Whiz in Training
HI all, I was wondering if anybody can help me to understand this exercise:
"A small airline has just purchased a computer for its new automated reservation system. Write an application to assign seas on each flight of the airline's only plane
(capacity: 10 seats).
Your application should display the following alternatives: 'Please type 1 for First class and please type 2 for economy'. If the user types 1, your application
should assign a seat in the first class section (seats 1-5), if he types 2 in the economy section ( seats 6-10). Your application should then display
a boarding pass indicating the person's seat number and whether it's in the first-class or economy.
Use a 1 dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements to false to indicate that all the
seats are empty. As each seat is assigned set the corresponding element of the array to true to indicate that the seat is no longer available. Your application
should never assign a seat that has already been assigned. When the economy section is full ask the person if it is acceptable to be placed in the first class section
and vice versa. If yes make the appropriate sear assignment. If not display the message 'next flight leaves in 3 hrs'"
Ok so there are a few things that I am not sure about. But first let me show you …
Violet_82 89 Posting Whiz in Training
I think I will ask this in the backend section of the forum, sorry my fault, I should have thought this had to do more with backend than front end!
thanks anyways
Violet_82 89 Posting Whiz in Training
fab thanks for that
Violet_82 89 Posting Whiz in Training
well' I'd like a bit of theory first if that makes sense, I have mobile devices I can test it on
Violet_82 89 Posting Whiz in Training
Hi no worries, I just thought I might ask. Well, I am planning to build the site and then upload pictures from both my laptop and straight from android, that's in a nutshell. I mean, it doestn' have to be Wordpress, but having an account with them already, having used it once before and knowing about good blog-like capabilities I tohught that it would be a good choice. AN android app is well beyond my skills, so what I was looking for is a kind of plug in, or whatever that allows me to quickly upload a picture from the phone to the website, as I said in my previous post, a bit like the facebook/twitter apps do on a smartphone. I would imagine there is a plug in of some sort out there somewhere, I mean surely somebody else must have done this already, it's just a matter of find out who : -)
Violet_82 89 Posting Whiz in Training
Chaps, I am doing a bit of responsive/mobile development. SOme concepts are pretty new to me, like using viewports, initial-scale, maximum-scale etc etc, so I was wondering if anybody is aware of a good tutorial that could clear these things up for me. I have found this quite beneficial http://alistapart.com/article/fluidgrids but I need to learn how to use the viewport more than anything.
cheers