public class Student {
private String name;
private String telNo;
private int age;
private double weight;
public Student(String name,String telNo,int age,double weight){
this.name = name;
this.telNo = telNo;
this.age = age;
this.weight = weight;
}
public String toString() {
String str;
str = "Name : " + name + "\n" +
"TelNo : " + telNo + " \n" +
"age : " + age + " year old\n"+
"Aeight : " + weight + " Kgs.\n";
return(str);
}
public String getName(){
return name;
}
public String getTelNo(){
return telNo;
}
public int getAge(){
return age;
}
public double getWeight(){
return weight;
}
public void setName(String name){
this.name=name;
}
public void setTelNo(String telNo){
this.telNo=telNo;
}
public void setAge(int age){
this.age=age;
}
public void setWeight(double weight){
this.weight=weight;
}
public static void main(String[] args) {
Student s1 = new Student("B","0812345678",20,89.1);
Student s2 = new Student("V","08123456789",19,91.2);
System.out.println("Detail B\n" +s1);
System.out.println("Detail V\n" +s2);
s1.setName("สมปอง");
s1.setTelNo("0869876543");
s2.setAge(21);
s2.setWeight(70);
System.out.println("name s1 :" +s1.getName());
System.out.println("TelNo s1 :" +s1.getTelNo());
System.out.println("Age s2 :" +s2.getAge());
System.out.println("Weight s2 :" +s2.getWeight());
// try: remove method toString() and see what happends
}
}
Tuesday, May 8, 2007
Program book
public class Book {
public static void main(String[] args) {
DetailBook B1 = new DetailBook("Book Java","Learn OOP",300,"974-71736-6-X",2005);
DetailBook B2 = new DetailBook("Book C++","Method",250,"854-33730-5-G",2006);
DetailBook B3 = new DetailBook("Book C++","Class",250,"553-62450-8-H",2006);
System.out.println("Book 1\n" +B1);
System.out.println("Book 2\n" +B2);
System.out.println("Book 3\n" +B3);
B1.setName("Management");
B1.setAuthor("KM(Knowled Management)");
B1.setPrice(1000);
B1.setIsbn("000-63250-1-W");
B1.setCopyNumber(2007);
System.out.println("name Book1 :" +B1.getName());
System.out.println("Author Book1 :" +B1.getAuthor());
System.out.println("Price Book1 :" +B1.getPrice()+" Baht");
System.out.println("Isbn Book1 :" +B1.getIsbn());
System.out.println("CopyNumber Book1 :" +B1.getCopyNumber());
}
}
public static void main(String[] args) {
DetailBook B1 = new DetailBook("Book Java","Learn OOP",300,"974-71736-6-X",2005);
DetailBook B2 = new DetailBook("Book C++","Method",250,"854-33730-5-G",2006);
DetailBook B3 = new DetailBook("Book C++","Class",250,"553-62450-8-H",2006);
System.out.println("Book 1\n" +B1);
System.out.println("Book 2\n" +B2);
System.out.println("Book 3\n" +B3);
B1.setName("Management");
B1.setAuthor("KM(Knowled Management)");
B1.setPrice(1000);
B1.setIsbn("000-63250-1-W");
B1.setCopyNumber(2007);
System.out.println("name Book1 :" +B1.getName());
System.out.println("Author Book1 :" +B1.getAuthor());
System.out.println("Price Book1 :" +B1.getPrice()+" Baht");
System.out.println("Isbn Book1 :" +B1.getIsbn());
System.out.println("CopyNumber Book1 :" +B1.getCopyNumber());
}
}
Test Password Weak or Good
public class Passwd {
public static void main(String[] args) {
int i,couter1=0,couter2=0;
String str;
System.out.print("Enter a String : ");
str = Console.readString();
if(str.length()>=8){ //Length Word Input > 8
int word1=str.indexOf("computer");
int word2=str.indexOf("mut");
if(word1>=0||word2>=0){//check word not have computer or mut
System.out.println("Weak Password");
}
else{
for(i=0;i if(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z'){
couter1++;
}
else
if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
couter2++;
}
}
if(couter1==str.length()||couter2==str.length()){
System.out.println("Weak Password");
}
else
System.out.println("Good Password");
}
}
else //Length Word Input < 8
System.out.println("Weak Password");
/* +++++++++++++++ Test Couter +++++++++++++++++++++ */
//System.out.println("Couter1: "+couter1);
//System.out.println("Couter2: "+couter2);
}
}
public static void main(String[] args) {
int i,couter1=0,couter2=0;
String str;
System.out.print("Enter a String : ");
str = Console.readString();
if(str.length()>=8){ //Length Word Input > 8
int word1=str.indexOf("computer");
int word2=str.indexOf("mut");
if(word1>=0||word2>=0){//check word not have computer or mut
System.out.println("Weak Password");
}
else{
for(i=0;i
couter1++;
}
else
if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
couter2++;
}
}
if(couter1==str.length()||couter2==str.length()){
System.out.println("Weak Password");
}
else
System.out.println("Good Password");
}
}
else //Length Word Input < 8
System.out.println("Weak Password");
/* +++++++++++++++ Test Couter +++++++++++++++++++++ */
//System.out.println("Couter1: "+couter1);
//System.out.println("Couter2: "+couter2);
}
}
Program Multiply
public class Multiply {
public static void main(String[] args) {
int i,j;
for(j=1;j<=12;j++){
for(i=1;i<=10;i++){
if((i*j)<10){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
else
if((i*j)>=10&&(i*j)<=99){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
else
if((i*j)>=100&&(i*j)<=144){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int i,j;
for(j=1;j<=12;j++){
for(i=1;i<=10;i++){
if((i*j)<10){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
else
if((i*j)>=10&&(i*j)<=99){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
else
if((i*j)>=100&&(i*j)<=144){
System.out.print(+i+"x"+j+"=");
System.out.print(" ");
System.out.print((i*j)+" ");
}
}
System.out.println();
}
}
}
Counts the number of bytes in a file
#include
#include
#include
main( )
{
FILE* fptr;
char extension[] = ".c";
char file_name[FILENAME_MAX]; /* defined in stdio.h*/
int char_count;
printf("\n\n\tFile name (NO extension):\t");
scanf("%s",file_name);
strcat(file_name,extension);
fptr=fopen(file_name,"rb");
for(char_count=0; getc(fptr) !=EOF; ++char_count);
printf("\n\tByte size:\t%d", char_count);
fclose(fptr);
return EXIT_SUCCESS;
}
#include
#include
main( )
{
FILE* fptr;
char extension[] = ".c";
char file_name[FILENAME_MAX]; /* defined in stdio.h*/
int char_count;
printf("\n\n\tFile name (NO extension):\t");
scanf("%s",file_name);
strcat(file_name,extension);
fptr=fopen(file_name,"rb");
for(char_count=0; getc(fptr) !=EOF; ++char_count);
printf("\n\tByte size:\t%d", char_count);
fclose(fptr);
return EXIT_SUCCESS;
}
Weight conversion of pounds to ounces
#include
main( )
{
int pounds;
/* Read first weight in pounds */
printf("\n\n\tWeight in pounds?");
scant("%d",£s);
/* Loop until user signals halt with negatie integer. */
while(pounds>=0)
{
printf("\n\n\tEquivalent weight in ounces: %d", pounds * 16);
printf("\n\n\tWeight in pounds?");
printf("\n\n\tEnter a negative integer to quit.");
scanf("%d",£s);
}
}
main( )
{
int pounds;
/* Read first weight in pounds */
printf("\n\n\tWeight in pounds?");
scant("%d",£s);
/* Loop until user signals halt with negatie integer. */
while(pounds>=0)
{
printf("\n\n\tEquivalent weight in ounces: %d", pounds * 16);
printf("\n\n\tWeight in pounds?");
printf("\n\n\tEnter a negative integer to quit.");
scanf("%d",£s);
}
}
Sorting program
/* Using qsort() and bsearch() with values.*/
#include
#include
#define MAX 20
int intcmp(const void *v1, const void *v2);
main()
{
int arr[MAX], count, key, *ptr;
/* Enter some integers from the user. */
printf("Enter %d integer values; press Enter after each.\n", MAX);
for (count = 0; count < MAX; count++)
scanf("%d", &arr[count]);
puts("Press a key to sort the values.");
getch();
/* Sort the array into ascending order. */
qsort(arr, MAX, sizeof(arr[0]), intcmp);
/* Display the sorted array. */
for (count = 0; count < MAX; count++)
printf("\narr[%d] = %d.", count, arr[count]);
puts("\nPress a key to continue.");
getch();
/* Enter a search key. */
printf("Enter a value to search for: ");
scanf("%d", &key);
/* Perform the search. */
ptr = (int *)bsearch(&key, arr, MAX, sizeof(arr[0]),intcmp);
if ( ptr != NULL )
printf("%d found at arr[%d].", key, (ptr - arr));
else
printf("%d not found.", key);
}
int intcmp(const void *v1, const void *v2)
{
return (*(int *)v1 - *(int *)v2);
}
#include
#include
#define MAX 20
int intcmp(const void *v1, const void *v2);
main()
{
int arr[MAX], count, key, *ptr;
/* Enter some integers from the user. */
printf("Enter %d integer values; press Enter after each.\n", MAX);
for (count = 0; count < MAX; count++)
scanf("%d", &arr[count]);
puts("Press a key to sort the values.");
getch();
/* Sort the array into ascending order. */
qsort(arr, MAX, sizeof(arr[0]), intcmp);
/* Display the sorted array. */
for (count = 0; count < MAX; count++)
printf("\narr[%d] = %d.", count, arr[count]);
puts("\nPress a key to continue.");
getch();
/* Enter a search key. */
printf("Enter a value to search for: ");
scanf("%d", &key);
/* Perform the search. */
ptr = (int *)bsearch(&key, arr, MAX, sizeof(arr[0]),intcmp);
if ( ptr != NULL )
printf("%d found at arr[%d].", key, (ptr - arr));
else
printf("%d not found.", key);
}
int intcmp(const void *v1, const void *v2)
{
return (*(int *)v1 - *(int *)v2);
}
Subscribe to:
Posts (Atom)