user43529463
0
Q:

class in java

public class Lightsaber {
  // properties
  private boolean isOn;
  private Color color;
  
  // constructor
  public Lightsaber(Color color) {
    this.isOn = false;
    this.color = color;
  }
  
  // getters
  public Color getColor() {
    return color;
  }
  public boolean getOnStatus() {
    return isOn;
  }
  
  // setters
  public void turnOn() {
    isOn = true;
  }
  public void turnOff() {
    isOn = false;
  }
}



// Implementation in main method:
public class test {
  public static void main(String[] args) {
    Lightsaber yoda = new Lightsaber(green);
    yoda.turnOn();
  }
}
    
7
public class HelloWorld {
  public static void main(String[] args) {
   
   
   class Number{
   
   	int number;
   
   	public boolean isPos(){
   		if(number > 0){
   			return true;
   		} else {
   			return false;
   		}
   		
   	}
   }
   
   Number myNumber = new Number();
   
   myNumber.number = 7;
   
   if(myNumber.isPos()){
   	System.out.println(myNumber.number + " is positive!!!");
   } else {
   	System.out.println(myNumber.number + " is not positive!!!");
   }
   
  }
}
2

public class HelloWorld {
  public static void main(String[] args) {
    
    // how to use class in java
    class User{
    	
    	int score;
    	
    	// method that can be used outside the class
    	public boolean hasWon(){
    		if(score >= 100){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    User dave = new User();
    
    dave.score = 20;
    
    System.out.println(dave.hasWon());
    
 
  }
}
2
// this might help you understand how classes work
public class MathTest {

    public static void main(String[] args) {

        class MathAdd {

            int num1;
            int num2;

            public int addNumbers() {

                int addThemUp = num1 + num2;
                return addThemUp;
            }
        }

        MathAdd addition = new MathAdd(); // create a new instance of the class

        // you can access variables from the class 
        addition.num1 = 10; 
        addition.num2 = 20;

        // and use the method from the class to add them up
        System.out.println(addition.addNumbers());

    }
}
1
class Number {
    	
    	int number;
    	
    	// square numbers
    	public boolean isSquare(){
    		
    		double squareRoot = Math.sqrt(number);
    		
    		
    		if(squareRoot == Math.floor(squareRoot))
    		{
    			return true;
    		} else {
    			return false;
    		}
    		
    	}
    	
    	// triangular numbers
    	public boolean isTriangular(){
    		
    		int x = 1;
    		
    		int triangularNumber = 1;
    		
    		while(triangularNumber < number){
    			
    			x++;
    			triangularNumber = triangularNumber + x;
    			
    		}
    		
    		if(triangularNumber == number){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    // testing 
    Number myNumber = new Number();
    myNumber.number = 16;
    
    System.out.println(myNumber.isSquare());
1
//	Example : TestClass (Can be predefined or user-defined)
public class TestClass {
  // properties
  private int id = 111;
  
  // constructor
  public TestClass(){
    super();
  }
  
  // method
  public void test(){
    // some code here
  }
}
2

public class HelloWorld {
  public static void main(String[] args) {
    
    // how to use class in java
    class User{
    	
    	int score;
    }
    
    User dave = new User();
    
    dave.score = 20;
    
    System.out.println(dave.score);
    
 
  }
}
1
Class is a blueprint or template which you can create as many objects as you 
like. Object is a member or instance of a class.
Class is declared using class keyword, Object is created through
new keyword mainly. A class is a template for objects. A class defines 
object properties including a valid range of values, and a default value. 
A class also describes object behavior.
0
abstract class Pesan {
   public void success() {
     System.out.println("Mobil Berhasil Dibeli");
   }
   
   public void error() {
     System.out.println("Uang Anda Tidak Cukup");
   }
}
 
class Car extends Pesan {
    protected String nama = "toyota supra";
    protected String warna = "merah";
    protected int harga = 2000000000;
    protected String brand = "toyota";
}
 
class ShowRoom extends Car {
  protected String namaShowroom = "Catur Sentosa Raya";
  protected String alamatShowroom = "Jl.siliwangin kec pancoranmas kota depok 16436";
}
 
class Pembeli extends ShowRoom {
    protected String namaPembeli = "anto jayabaya";
    protected String alamatPembeli = "jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";
    protected int saldoPembeli = 50000000;
}
 
class BeliMobil extends Pembeli {
 
 public BeliMobil(String nama, String warna, int harga, String brand, String nsr, String asr, String np, String ap, int sdp) {
    super();
    
    super.nama = nama;
    super.warna = warna;
    super.harga = harga;
    super.brand = brand;
    super.namaShowroom = nsr;
    super.alamatShowroom = asr;
    super.namaPembeli = np;
    super.alamatPembeli = ap;
    super.saldoPembeli = sdp;
 }
    
void getResult(String nama, String warna, int harga, String brand, String np, String ap) {
    
     if(super.harga > super.saldoPembeli) {
       System.out.println("=======================");
       super.error();
       System.out.println("=======================");
     } else {
       System.out.println("=======================");
       super.success();
       System.out.println("=======================");
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Jenis Mobil");
       System.out.println("=======================");
       System.out.println("");
       System.out.println("Nama Mobil:" + nama);
       System.out.println("Warna Mobil:" + warna);
       System.out.println("Harga Mobil:" + harga);
       System.out.println("Brand Mobil:" + brand);
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Nama Pembeli Mobil");
       System.out.println("=======================");
       System.out.println("Nama Pembeli:" + np);
       System.out.println("Nama Pembeli:" + ap);
     }
  }
    
 public static void main(String[] args) {
   BeliMobil beli = new BeliMobil("avanza", "hitam", 128000000, "toyota", "Jaya Mobil", "Jakarta", "Anton", "Depok", 228000000);
   beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);
  }
}
 
0

New to Communities?

Join the community