Dasturlash uslublari va paradigmalar
Download 199.08 Kb. Pdf ko'rish
|
Ilyosov OO
- Bu sahifa navigatsiya:
- Java dasturlash tilida dastur ko’di
310-18 guruh talabasi Ilyosov Otabek “Dasturlash uslublari va paradigmalar” fanidan bajargan 3-topshirig’i
16-variant 1) To’rt o'zgaruvchiga ega bo'lgan sinf yarating. Displey funktsiyasini va ushbu o'zgaruvchilarni o'zgartirish funksiyasini qo'shing. Ushbu o'zgaruvchilar qiymatlari yig'indisini topadigan va shu to’rt o'zgaruvchining eng katta qiymatini topadigan funksiyani qo'shing. Java dasturlash tilida dastur ko’di import
java.util.Random ; import java.util.Scanner ;
public class Task1
{
int var1
;
int var2
;
int var3
;
int var4
;
public Task1
() {
System .
.print( "O'zgaruvchilarning qiymatlarini kiriting \n 1: "
);
Scanner scanner = new Scanner( System
. in );
var1
= scanner
.nextInt();
System .
.print( "2: "
);
var2 = scanner .nextInt();
System .
.print( "3: "
);
var3 = scanner .nextInt();
System .
.print( "4: "
);
var4 = scanner .nextInt(); }
public void setVar1
( int
var1) {
this . var1 = var1; }
public void setVar2
( int
var2) {
this . var2 = var2; }
public void setVar3
( int
var3) {
this . var3 = var3; }
public void setVar4
( int
var4) {
this . var4 = var4; }
public String
display () {
return
"Task1{" +
"var1=" + var1 +
", var2=" + var2 +
", var3=" + var3 +
", var4=" + var4 +
'}' ;
public int getSum (){
return
var1 + var2 + var3
+ var4
; }
public int getMax
(){
return Math
.max( Math
.max( var1
, var2
), Math
.max( var3
, var4
)); }
}
class Main {
public static void main
( String
[] args) {
Random random = new Random();
Task1 task1 = new Task1();
System .
.println( task1
.display());
System .
.println( "Max: "
+ task1
.getMax());
System .
.println( "Sum: "
+ task1
.getSum());
task1 .setVar1( random .nextInt( 20 ));
task1
.setVar2( random
.nextInt( 20 )); task1
.setVar3( random
.nextInt( 20 )); task1
.setVar4( random
.nextInt( 20 )); System
. out .println( task1 .display()); } }
Natija 2) Belgilangan diapazonda qiymatini bittaga oshirishi yoki kamaytirishi mumkin bo'lgan o'nli hisoblagichni amalga oshiradigan sinfni tavsiflang. Standart qiymatlar va ixtiyoriy qiymatlar bilan hisoblagichni ishga tushirishni ta'minlang. Hisoblagich ikkita usulga ega bo’lishi kerak: • oshirish va kamaytirish; • hozirgi holatini olishga imkon berish. Java dasturlash tilida dastur ko’di import
java.util.Random ;
public class Task2
{
public int var
;
public Task2
() {
Random random = new Random();
var = random .nextInt( 40 ); }
public Task2
( int
var) {
this . var = var; }
public int getValue
(){
return var
; }
public void increaseValue (){
var ++;
}
public void decreaseValue (){
var --;
} }
class Main2
{
public static void main
( String
[] args) {
Task2 task2 = new Task2();
System .
.println( "Initial value: " + task2
.getValue());
task2 .increaseValue();
System
. out .println( "After increasing value: " + task2 .getValue());
task2 .decreaseValue();
task2
.decreaseValue();
task2 .decreaseValue();
System
. out .println( "After decreasing value 3 times: " + task2 .getValue()); }
} Natija Download 199.08 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling