Monday, May 24, 2010

JAVA Making a CHAR equal a Number for an exercise creating GRADEBOOK. 2nd Post with CODE?

Possibly have MANY errors, but for now I'm concerned with the IF statement:


import javax.swing.*;


import java.lang.String;


import java.io.*; // This imports the File class


public class Lab10


{


public static void main (String args[])throws Exception


{


String StudentCountStr, SnameStr,Snumr,courseStr,lgradeStr,Grade...


Double nGrade;


FileWriter fw = new FileWriter("GradesTable.txt");


BufferedWriter bw = new BufferedWriter(fw);


PrintWriter pw = new PrintWriter(bw);


customerCountStr = JOptionPane.showInputDialog(null, "How many Customers to enter?");


StudentCountStr = JOptionPane.showInputDialog(null, "How many Student Grades to enter?");


/* Here the instructor will enter the number of students that must be entered.


Below, the string will be parsed to a numeric value for calculations */


SnameStr = JOptionPane.showInputDialog(null, "Enter Student Name");


Snumr = JOptionPane.showInputDialog(null, "Enter Student Number");


courseStr = JOptionPane.showInputDialog(null, "Enter Course Name %26amp; Section:");


lgradeStr = JOptionPane.showInputDialog(null, "Enter Letter grade: (no + or -");





// Grades are assigned as follows: A=4,B=3,C=2,D=1,F=0


Double GradeCount = Double.valueOf(GradeCountStr);





/* The syntax for the FOR loop is:


for(%26lt;initialization%26gt;; %26lt;condition%26gt;; %26lt;update%26gt;)


{ %26lt;executable statements%26gt; } */


for(int loopCount = 1; loopCount %26lt;= GradeCount; ++loopCount)





/* Here loopCount is initiated to 1 so as to never have a ZERO at the beginning. Then it is compared to


GradeCount. If loopCount is less than or equal to GradeCount, the program will go on %26amp; 1 will be added


to the value of loopcount. When loopCount is GREATER than GradeCount, the program will ask if there's


another student, if not it'll end.*/


{


if(lgradeStr.equals('A')) {nGrade = 4;}


else if (lgradeStr.equals(‘B’)) { nGrade = 3; }


else if (lgradeStr.equals(‘C’)) { nGrade = 2; }


else if (lgradeStr.equals(‘D’)) { nGrade = 1; }


else {ngrade = 0;}}


pw.println(" " + lgrade + " " + Gradepoints);


{


}


{


pw.println("Bucks County Community College Grade Report For");//this places the heading


pw.println(SnameStr);


pw.println(Snumr);


pw.println(" ");


pw.println(" ");


pw.println(" Course " + " Grade "+"Gradepoints");//This places the SubHeadings


}


pw.close();


}


}

JAVA Making a CHAR equal a Number for an exercise creating GRADEBOOK. 2nd Post with CODE?
This is quite simple, just use an if statement.





something like this:








char a = in.nextChar();


char b = 'b'


int i = (anything);


if(a == b)


{


i = 1;


}





and so on
Reply:I'm not to fmiliar with Java, but it sounds like you need to compare 2 strings (declare nGrade as a variable) to display 4 for A, 3 for B.... but that won't get you the GPA becaus eyou won't be able to ADD. Sorry


Accessing bits in C?

Say I have a bitfield like this:





typedef struct


{


unsigned char bit0:1;


unsigned char bit1:1;


unsigned char bit2:1;


unsigned char bit3:1;


unsigned char bit4:1;


unsigned char bit5:1;


unsigned char bit6:1;


unsigned char bit7:1;


}io_reg;





now I want to assign a unsigned char value to a io_reg type data so that I can access the bits of it. How can I do that? Please help me.

Accessing bits in C?
You can always create a union:





union {


unsigned char val;


typedef struct {


unsigned char bit0:1;


unsigned char bit1:1;


unsigned char bit2:1;


unsigned char bit3:1;


unsigned char bit4:1;


unsigned char bit5:1;


unsigned char bit6:1;


unsigned char bit7:1;


} bits;


} io_reg;





In this way, if you want to assign the entire unsigned char value, you can just use:





io_reg regUnion;


regUnion.val = value;





If you want to access the bits just do this:





regUnion.bits.bit0





etc.

survey for cash

Another C++ Question?

Sort and output an array of inputted characters, based on their number of occurrences (from highest to lowest). What's wrong with this code?





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





int main(void)


{


using namespace std;





char chars[]="abcdefghijklmnopqrstuvwxyz";


int i;


int count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0...





char inp;





cin%26gt;%26gt;inp;





while(inp !='.')


{


for(i=0;i%26lt;26;++i)


{


if(inp==chars[i])


count[i]++;


}


cin%26gt;%26gt;inp;


}





int swap;


char swapchar;





for(i=0; i%26lt;26; ++i)


{


if (count[i] %26gt; count[i + 1])


{


swap = count[i];


count[i] = count[i + 1];


count[i + 1] = swap;





swapchar = chars[i];


chars[i] = chars[i + 1];


chars[i + 1] = swapchar;


}


}





cout%26lt;%26lt;"Character Count"%26lt;%26lt;endl;








for(i=0;i%26lt;26;++i)


{


if(count[i]!=0)


{


cout%26lt;%26lt;chars[i]%26lt;%26lt;" "%26lt;%26lt;count[i]%26lt;%26lt;endl;


}


}





system("pause");





return 0;


}

Another C++ Question?
You put your using statement inside main().
Reply:Well first of all your namespace position is incorrect... it should be after #include%26lt;fstream%26gt;, you don't need fstream since you are not doing any file streaming.





Second, check your braces... { and } i tried fixing your program and it doesnt work actually because of the errors... your swap or as we call it "Bubble sort" is ok... but change the limit from 26 to 25 since you'll be comparing the second to the last. Also, you'll have to put that in another loop since you need to do that 25 times again since you'll only be pushing one lowest number at a time... you should also try considering if there are equal times it appeared.





Note: I didn't fix it to work... i want you to handle that part. ^^, i'm just here to correct a few errors =)


Must allow for the char w to = 0?

When I test the code with 0000071w it reads false which is not correct. I then added the condition (letter == 0.) This is not correct. This code will give a false reading to all numbers?


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;title%26gt; PPS Checksum %26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;script type="text/javascript"%26gt;





var checknum=prompt("Please enter your PPS number","");


document.write("%26lt;br/%26gt; You entered " + checknum);


if(checknum!=null %26amp;%26amp; checknum!="")


{


var sum = 0;


for ( var i = 0; i %26lt; 7; i++)


{


var c = checknum.charCodeAt(i);


c = c- 48;


sum = sum + c *( 8-i);





}


checknum = checknum.toUpperCase();


letter = checknum.charCodeAt(7);


letter = letter - 64;


pos = sum % 23;


}





if ( pos == letter)||(letter== 0)





document.write("%26lt;br/%26gt; PPS number is valid");





else





document.write("%26lt;br/%26gt; PPS number is false");








%26lt;/script%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;

Must allow for the char w to = 0?
Hi


May I suggest you subtract (64 + 23) i.e. 87 from 'letter' not just 64 and remove the 'letter==0' from the if statement.


Hope this helps.


Regards


g


C++ "=" operator overloading?

I have following class, I want to overload “=” operator, I did as following but I cant get success, any one can help me please


class String


{


private:


char * name;


public:


const char * getName() const { }


void setName(const char * aname) { }


friend char * operator=(char %26amp; str, const String %26amp;rhs );


}





char * operator= (char %26amp;str, const String %26amp;rhs )


{


str = const_cast%26lt;char * %26gt;(rhs.getName())


return *str;


}





Void main()


{


String obj;


Obj.setName(“Hello World “);


Char * myString = Obj;


Cout%26lt;%26lt;myString;


}

C++ "=" operator overloading?
Sure.





Your problem here is that the parameters for your operator = are (char %26amp;, String %26amp;). You have two problems here.





First, you want to make that char *%26amp;, not char %26amp;. As defined here, you have an operator = that copies a string into a single character.





The second problem is a little bigger. You are copying a pointer here instead of doing any sort of copying of contents. When done, myString wand Obj will point at the SAME string.





While this makes for a very fast copy, you're almost guaranteed to have problems later:





1. Notice what happens to myString when Obj is deleted. myString is now pointing at deleted memory. The reverse is true as well.





2. If myString changes, so does Obj. The reverse is true as well.





3. There is no safe way to destroy the name * in your string object because you don't know how many copies were made, and if those are still in use. I'm assuming here that the implementation of setName() will allocate new memory for name, and that your full version has a destructor.





I hope this helps. Good luck.
Reply:You have completely mixed your metaphors here.





First off, your operator = only needs to take one parameter, not two -- and that parameter is whatever type you want to be able to assign TO your String.





Second, your operator = should return a String%26amp;.





Third, in order to assign a String object to a char*, you need to overload the char* cast operator.





And finally, make sure you understand that char* is just a pointer -- to actually put anything IN your string, you're going to have to dynamically allocate memory.


Need help with c-programming; strsr and other?

Write a function that takes as input a string containing HTML source code and returns both the # of images referenced in the code and the total screen area (in square pixels) occupied by the images


The function MUST have the following declarationint estImageContent(char *pacHTML, long int *pliArea);








1. Prompt the user for an input filename


2. Open the file, read its contents into a string (up to 20000 bytes), close file


3. Call estImageContent()


4. Print the following to the screen:


1. Input filename


2. Estimated # of images


3. Estimated total screen area in square pixels


5. Loop back to prompt





this is what i have so far


/* the first thing i did was uppercase*/





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;ctype.h%26gt;








void fileToString(char filename[], char theString[]);


char *upperCaseTheString(char *pc);





int main()


{


char filename[] = "p6in1.txt";


char theString[20000] = "";





fileToString(filename, theString);


upperCaseTheString(theString);





printf("%s", theString);





return (0);


}








void fileToString(char filename[], char theString[]) {


FILE *fp;


char buf[BUFSIZ+1];





fp = fopen(filename, "r");





while (fgets(buf, BUFSIZ, fp) != NULL) {


strcat(theString, buf);


}





fclose(fp);





}








char *upperCaseTheString(char *pc) {


char *pcReturn = pc;





while (*pc != '\0') {


*pc = toupper(*pc);


pc++;


}





return pcReturn;


}


fp=fopen("p6in.txt","r");





if(fp==NULL)


{


printf("\n\t\tSORRY......\n\t\t CANNOT OPEN FILE\n");


return 0;


}





printf("\n\t\tFILE data.txt FOUND!\n");


printf("\n\nSearch what string?\t");


gets(str1);





while ((fgets( str2, 100, fp )) != NULL) //not the end of file


{


if (strcmp(str1, str2)


iCount++;











}


printf("\n%d OCCURRENCES OF STRING %s FOUND", iCount ,str1);








}

Need help with c-programming; strsr and other?
Here is a link with info about strstr:


http://www.cppreference.com/stdstring/st...
Reply:I don't know

customer satisfaction survey

C++ Question?

[code]


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int read ( char , char );





typedef struct


{


int computernum;


char fibit[8];


char sebit[8];


char thbit[8];


char fobit[8];





} address;





main ()


{


char red;


red = read (zero, one);





cout %26lt;%26lt; red %26lt;%26lt; endl;


return 0;


}





int read (char z, char o)


{


ifstream fin;


char zero;


char one;


fin.open ("prog2.dat");





if (fin.fail() )


{


cout %26lt;%26lt; "File not found or opened" %26lt;%26lt; endl;


exit(1);


}





if ( zero = 0)


{


zero= 0;


}


return 0;


}


[/code]





I am lost on the last part mostly. My instructor gave me a file named prog2.dat which contained this file





"


00001110011110010000101001011010


00000011000000110000011000000110


10010010001111110101101001010101


00000001000000010000000100000001


00000111001010111101010101110101


00000000101011110101010101110

C++ Question?
You haven't asked any question. All you've done is post some source code and input file info. What is the code supposed to do? What are you having trouble figuring out?
Reply:since int read(..) return only zero .....looks like your program will only output zeros....


so what is your question?