PDA

View Full Version : سوال: تکمیل یک برنامه ساده



yegane8
شنبه 23 اردیبهشت 1391, 20:33 عصر
سلام
این برنامه که گذاشتم دو تا کلاسن که باید اگه کاربر جمله hello java world رو به عنوان جمله و کلمه java رو به عنوان کلمه وارد کرد در خروجی برنامه hello JAVA world چاپ شه ولی در این سورسی که من نوشتم فقط اگه کلمه java در جمله وجود داشت کلمه JAVA رو چاپ میکنه میشه دقیقا توضیح بدین که باید چی کار کنم تا کلمه های دیگه هم به همووون ترتیب که تو جمله بودن هم چاپ شن .
کلاس 1 :
import java.util.Scanner;
/**
* Write a description of class input here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class input
{
// instance variables - replace the example below with your own
private Scanner reader;

/**
* Constructor for objects of class input
*/
public input()
{
// initialise instance variables
reader = new Scanner(System.in);
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String getSentence()
{

System.out.println("\nenter the sentence");
String sentence = reader.nextLine();
return sentence;

}
public String getWord()
{

System.out.println("\nenter the word u want to be capital");
String word = reader.nextLine();
return word;

}
}
کلاس 2:

import java.util.ArrayList;
import java.lang.String;
/**
* Write a description of class parser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class parser
{
// instance variables - replace the example below with your own
private input inputer;


/**
* Constructor for objects of class parser
*/
public parser()
{
// initialise instance variables
inputer = new input();

}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void search(){
String sentec = inputer.getSentence();
String word = inputer.getWord();
if(sentec.contains(word)){
String[] wordArray = sentec.split(" ");

int m = sentec.indexOf(word);

System.out.println("exist");
// String[] wordArray = sentec.split(" ");
word.toUpperCase();
System.out.println(word.toUpperCase());
}

}
}

spiderman200700
شنبه 23 اردیبهشت 1391, 21:08 عصر
سلام.
اگه سوالتونو درست فهمیده باشم، شما همچین برنامه ای میخواید.
import java.util.Scanner;

/**
* Write a description of class input here.
*
* @author (your name)
* @version (a version number or a date)
*/
class input {
// instance variables - replace the example below with your own

private Scanner reader;

/**
* Constructor for objects of class input
*/
public input() {
// initialise instance variables
reader = new Scanner(System.in);
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String getSentence() {

System.out.println("\nenter the sentence");
String sentence = reader.nextLine();
return sentence;

}

public String getWord() {

System.out.println("\nenter the word u want to be capital");
String word = reader.nextLine();
return word;

}
}

/**
* Write a description of class parser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class parser {
// instance variables - replace the example below with your own

private input inputer;

/**
* Constructor for objects of class parser
*/
public parser() {
// initialise instance variables
inputer = new input();
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void search() {
String sentec = inputer.getSentence();
String word = inputer.getWord();
if (sentec.contains(word)) {

System.out.println("exist");

sentec=sentec.replaceAll(word,word.toUpperCase());
System.out.println(sentec);
}

}

}

نکتشم اینجاس:
sentec=sentec.replaceAll(word,word.toUpperCase());

yegane8
شنبه 23 اردیبهشت 1391, 21:20 عصر
سلام.
اگه سوالتونو درست فهمیده باشم، شما همچین برنامه ای میخواید.
import java.util.Scanner;

/**
* Write a description of class input here.
*
* @author (your name)
* @version (a version number or a date)
*/
class input {
// instance variables - replace the example below with your own

private Scanner reader;

/**
* Constructor for objects of class input
*/
public input() {
// initialise instance variables
reader = new Scanner(System.in);
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String getSentence() {

System.out.println("\nenter the sentence");
String sentence = reader.nextLine();
return sentence;

}

public String getWord() {

System.out.println("\nenter the word u want to be capital");
String word = reader.nextLine();
return word;

}
}

/**
* Write a description of class parser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class parser {
// instance variables - replace the example below with your own

private input inputer;

/**
* Constructor for objects of class parser
*/
public parser() {
// initialise instance variables
inputer = new input();
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void search() {
String sentec = inputer.getSentence();
String word = inputer.getWord();
if (sentec.contains(word)) {

System.out.println("exist");

sentec=sentec.replaceAll(word,word.toUpperCase());
System.out.println(sentec);
}

}

}

نکتشم اینجاس:
sentec=sentec.replaceAll(word,word.toUpperCase());

مرسی
فقط string اولی چیه دقیقا ؟

spiderman200700
شنبه 23 اردیبهشت 1391, 21:24 عصر
sentec که جملتونه .
replaceAll میاد توی جمله میگرده تمام word ها رو پیدا میکنه و با ,word.toUpperCase() جایگزینشون میکنه و جمله ی جدید رو بازم توی sentec قرار میده.

M0TR!X
شنبه 23 اردیبهشت 1391, 22:09 عصر
سلام.

چیزی که من خوب نفهمیدم همیشه اینه که چطوری یه متغییر از نوع کلاس تعریف میکنید ؟ مثلا



private Scanner reader;


این الان دقیقا چه مقادیری میتونه بگیره ؟ و کی اصلا باید تعریف کنیم ؟

yegane8
شنبه 23 اردیبهشت 1391, 22:43 عصر
سلام.

چیزی که من خوب نفهمیدم همیشه اینه که چطوری یه متغییر از نوع کلاس تعریف میکنید ؟ مثلا



private Scanner reader;


این الان دقیقا چه مقادیری میتونه بگیره ؟ و کی اصلا باید تعریف کنیم ؟
به خاطر اینکه ما تو جاوا نمیتونیم از کلاس ها به طور مستقیم تو برناممون استفاده کنیم باید از کلاسمون یه object بگیریم که با تعریف کردن یه متغییر از همووون کلاس و با دستور new این کار رو انجام میدیم و هر وقت به کلاسی تو برنامت نیاز داشتی باید این کارو بکنی تا بتونی از متد های اوون کلاس استفاده کنی

spiderman200700
شنبه 23 اردیبهشت 1391, 22:55 عصر
یه کارخانه ی اتومبیل رو فرض کن. این کارخانه میاد اول نقشه ی اتومبیل رو میکشه و تمام کار هایی رو که قراره انجام بشه تا یه اتومبیل ساخته بشه رو توی این نقشه نشون میده.تا وقتی از روی این نقشه اتومبیلی ساخته نشه نمیشه سوارش شد و رانندگی کرد.
توی برنامه نویسی ، ما به این نقشه، کلاس میگیم.
حالا این کارخونه میاد از روی این نقشه nتا اتومبیل میسازه. حالا که اتومبیل ساخته شد میشه ازش استفاده کرد.
توی برنامه نویسی به این اتومبیل که از روی نقشه ساخته شده، نمونه ی واقعی ماشین یا Object یا متغیر از نوع کلاس یا یه نمونه ی واقعی از کلاس میگیم، که میشه ازش استفاده کرد.
پس کلاس بدون ساخت Object از اون، کاملا بی استفادس و Object بدون کلاس اصلا وجود نداره.

وقتی این دستور رو مینویسی
private Scanner reader;
یه متغییر تعریف میشه که آمادس تا به یه قسمت از حافظه که حاویه اطلاعات اون شی هست، اشاره کنه.

وقتی این دستور رو مینویسی
reader=new Scanner(System.in);
تازه اینجاس که میاد حجم تمام متغییر ها و ثابت های داخل کلاس Scanner رو محاسبه میکنه و به همون اندازه توی حافظه فضا میگیره و آدرس این فضا رو توی متغییر reader که ساخته بودیم میذاره.
چون متغییر reader از نوع Scanner هست، پس فقط میتونه مقادیری رو که از نوع Scanner یا مشتقات Scanner هستند رو بگیره.
یعنی میتونی یه متغییر دیگه از نوع Scanner تعریف کنی و توی متغییر reader قرارش بدی.
مثلا اینطوری:
Scanner reader2=new Scanner(System.in);
reader=reader2;

موفق باشی